473,416 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

structure alignment rules

Can somebody please tell me about the structure alignment rules ?
What I found was that on my system (cygwin running on PC, size of
int=4 sizeof long=4, size of long long = 8) the cygwin compiler put
the padding after the last member of structure.
For eg, struct test {
int i;
char c; /* no padding required between
int and char */
/* 3 byte padding is inserted here, Why ?
*/
}

When I print the size of above structure, it gives 8. I am not able to
understand why the 3 byte padding is done after char ?

Why the size of struct is made 4 byte aligned ?

Moreover, if I replace "int" by "long long", the size is 16. So, its
not always 4 byte aligned.

What is the rule, to find out how much padding will be added at the
end of structure ?
What is the byte alignment restriction for a structure ?
thanks a lot for any help ...

May 24 '07 #1
4 11186
ju**********@yahoo.co.in wrote:
Can somebody please tell me about the structure alignment rules ?
What I found was that on my system (cygwin running on PC, size of
int=4 sizeof long=4, size of long long = 8) the cygwin compiler put
the padding after the last member of structure.
For eg, struct test {
int i;
char c; /* no padding required between
int and char */
/* 3 byte padding is inserted here, Why ?
*/
}

When I print the size of above structure, it gives 8. I am not able to
understand why the 3 byte padding is done after char ?

Why the size of struct is made 4 byte aligned ?

Moreover, if I replace "int" by "long long", the size is 16. So, its
not always 4 byte aligned.

What is the rule, to find out how much padding will be added at the
end of structure ?
What is the byte alignment restriction for a structure ?
The answers to all your questions is "it's implementation defined".

--
Ian Collins.
May 24 '07 #2
On May 24, 12:54 pm, Ian Collins <ian-n...@hotmail.comwrote:
junky_fel...@yahoo.co.in wrote:
Can somebody please tell me about the structure alignment rules ?
What I found was that on my system (cygwin running on PC, size of
int=4 sizeof long=4, size of long long = 8) the cygwin compiler put
the padding after the last member of structure.
For eg, struct test {
int i;
char c; /* no padding required between
int and char */
/* 3 byte padding is inserted here, Why ?
*/
}
When I print the size of above structure, it gives 8. I am not able to
understand why the 3 byte padding is done after char ?
Why the size of struct is made 4 byte aligned ?
Moreover, if I replace "int" by "long long", the size is 16. So, its
not always 4 byte aligned.
What is the rule, to find out how much padding will be added at the
end of structure ?
What is the byte alignment restriction for a structure ?

The answers to all your questions is "it's implementation defined".

--
Ian Collins.- Hide quoted text -

- Show quoted text -
thanks for your help.
So, you mean to say, there's no restriction from the C standard
regarding the structure alignment. Different compilers may chose
different padding.
But, why a compiler would put a padding at the end of strcuture (on a
particular implementation) ? And why, sometimes the size is 4 byte
aligned and sometimes the size is 8 byte aligned. Can you please
explain it for a particular implementation (where size of int =4 and
size of long = 4, size of long long = 8 and int should be 4 bytes
aligned and long long should be 8 bytes aligned).

May 24 '07 #3
ju**********@yahoo.co.in wrote:

(long preamble)
On May 24, 12:54 pm, Ian Collins <ian-n...@hotmail.comwrote:
>junky_fel...@yahoo.co.in wrote:
Can somebody please tell me about the structure alignment rules ?
What I found was that on my system (cygwin running on PC, size of
int=4 sizeof long=4, size of long long = 8) the cygwin compiler put
the padding after the last member of structure.
For eg, struct test {
int i;
char c; /* no padding required between
int and char */
/* 3 byte padding is inserted here, Why ?
*/
}
When I print the size of above structure, it gives 8. I am not able to
understand why the 3 byte padding is done after char ?
Why the size of struct is made 4 byte aligned ?
Moreover, if I replace "int" by "long long", the size is 16. So, its
not always 4 byte aligned.
What is the rule, to find out how much padding will be added at the
end of structure ?
What is the byte alignment restriction for a structure ?

The answers to all your questions is "it's implementation defined".

--
Ian Collins.- Hide quoted text -

- Show quoted text -

thanks for your help.
So, you mean to say, there's no restriction from the C standard
regarding the structure alignment. Different compilers may chose
different padding.
But, why a compiler would put a padding at the end of strcuture (on a
particular implementation) ?
Because when two such structures follow one another in an array,
the second must be properly aligned for its fields. (Or because
it Feels Like It, but that's always true, so we'll say no more
about that.)
And why, sometimes the size is 4 byte
aligned and sometimes the size is 8 byte aligned.
Because the alignment requirements of different data types are
different.
Can you please
explain it for a particular implementation (where size of int =4 and
size of long = 4, size of long long = 8 and int should be 4 bytes
aligned and long long should be 8 bytes aligned).
You just explained it yourself!

--
"It is seldom good news." ~Crystal Ball~, /The Tough Guide to Fantasyland/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

May 24 '07 #4
"ju**********@yahoo.co.in" wrote:
>
Can somebody please tell me about the structure alignment rules ?
What I found was that on my system (cygwin running on PC, size of
int=4 sizeof long=4, size of long long = 8) the cygwin compiler put
the padding after the last member of structure.
For eg, struct test {
int i;
char c; /* no padding required between int and char */
/* 3 byte padding is inserted here, Why ? */
}
[...]

Well, the answer is "it's implementation specific". However,
consider the ramifications of not having the padding bytes at
the end for the following:

struct test foo[2];

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
May 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Big Slim | last post by:
I'm working on a web application that makes heavy use of CSS, but I would like users to have a degree of control over some of the classes and attributes. To accomplish this, I want to store my CSS...
6
by: n00dle | last post by:
hi, see if i want to copy the contents of a char arrary into a equally size struct, assuming that the structure has no holes, padding etc., i can use structure = *(t_structure *) array; but...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
5
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
5
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX {...
11
by: swapnaoe | last post by:
Hi, I would like to know why structure alignment rules were introduced. How does a structure with/without boundary alignments differ. Regards Swapna
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.