structure alignment rules

J

junky_fellow

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 ...
 
I

Ian Collins

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".
 
J

junky_fellow

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

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).
 
C

Chris Dollin

(e-mail address removed) wrote:

(long preamble)
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!
 
K

Kenneth Brody

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:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Need clarity on structure alignment 60
struct alignment 14
Padding involved 77
gcc alignment options 19
Alignment problems 20
Alignment in Struct 7
Memory alignment 53
Structure Size and Padding Byte Questions 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top