the foll code when run on gcc compiler gave 12 as output
but since sizeof int is 4 and char is 1 then why does it give 12 instead of 8 as output????Code:struct aa{char a:3;int b:30;char c:3;}; printf("%d",sizeof(struct aa));
the foll code when run on gcc compiler gave 12 as output
but since sizeof int is 4 and char is 1 then why does it give 12 instead of 8 as output????Code:struct aa{char a:3;int b:30;char c:3;}; printf("%d",sizeof(struct aa));
There is padding for alignment.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Member rearrangement may coax it to use a byte alignment instead of a word alignment.
Member rearrangement is rarely sufficient to coax a compiler to use byte alignment - some other settings (compilation options) are also needed. And that requires all functions in your program to be compiled in the same manner - mixing and matching functions compiled with different alignment settings rarely works.
On a (fairly) typical 32 bit system that aligns on 32 bit boundaries, it wouldn't make much difference in this case.
But, yeah, in general, arranging members in order of decreasing size is likely to result in smaller overall struct than other orderings.
But when I replace sizeof c with 2 bits then it gives 8 as output ..Can someone please explain how is this possible???