Bug
Mumit Khan
khan@xraylith.wisc.EDU
Thu Sep 30 19:57:00 GMT 1999
Samy <hunter@gnet.com.eg> writes:
> Hi ,
> I've gcc (egcs-2.91.66) running on Red Hat Linux 6.0 .
> and i've the following problem :
>
> struct test {
> char i; // one byte
> int b; // 4 bytes and total is 5
> bytes
> }test;
> int main (void)
> {
> printf("%d",sizeof(test));
>
> }
>
> and the result is
> 8 .
> so can you explain it please
> Thanks
It's not a bug; the compiler follows alignment requirements when it lays
out the struct members and you're seeing the effect of that. Here the
second member, an integer, is aligned to 4-byte boundary and you end up
with a total size of 8 bytes.
If you really need "packed" layout, see GCC documentation for the packed
attribute.
If you declare struct test as following, you'll get 5 bytes:
struct test {
char i; /* one byte */
int b; /* 4 bytes and total is 5 bytes */
} __attribute__((packed)) test;
It's a bad in general to expect a certain layout of struct members.
Regards,
Mumit
More information about the Gcc-bugs
mailing list