|
|
 |
 |
 |
 |
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 ...
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.
On May 24, 12:54 pm, Ian Collins <ian-n@hotmail.com> wrote:
> 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 - >
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).
junky_fel @yahoo.co.in wrote: (long preamble)
> On May 24, 12:54 pm, Ian Collins <ian-n @hotmail.com> wrote: >> 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 - >> > 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
"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 ? */ > }
[...] 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:ThisIsASpamT@gmail.com>
|
 |
 |
 |
 |
|