By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

structure alignment rules

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.