Differences in struct size when compiling C vs. C++

Andrea 'Fyre Wyzard' Bocci fwyzard@inwind.it
Thu Jan 17 04:20:00 GMT 2002


At 20.06 16/01/2002 (GMT -0800), inaky.gonzalez@intel.com wrote:

>$ gcc kk.c -o kk -Wall
>
>output is:
>   sizeof (struct A) 4, sizeof (struct B) 0
>
>$ g++ kk.c -o kk -Wall
>
>output is:
>   sizeof (struct A) 8, sizeof (struct B) 1
>
>
>Why? :) I would expect this if there were vtables around, but I am
>afraid there aren't any. Can anybody give me some insight? [pls copy
>me, as I am not subscribed to the list]
>
>     Thanks!
>
>PS: Great compiler, though :)
>
>
>-- begin kk.c...
>
>#include <stdio.h>
>
>struct A
>{
>   int d;
>   struct {} c;
>};
>
>struct B
>{
>   struct
>   {
>   } c;
>};
>
>int main (void)
>{
>   printf ("sizeof (struct A) %d, sizeof (struct B) %d\n",
>           sizeof (struct A), sizeof (struct B));
>   return 0;
>}

I think this is the same as in issue discussed a month ago on the gcc ML 
for classes (http://gcc.gnu.org/ml/gcc/2001-12/msg00836.html).
Shortly:
The empty struct sub-object must have a different address from the struct 
that contains it.
This is obvious with A, where "int d" between the two makes this happen 
automatically.
With B, the compiler must use an empty byte to let the struct {} c addres 
be different from that of B.
Then everything is alligned to 32 bit boundaries.
Well - I don't know why this doesn't happen with plai gcc. Maybe C doesn't 
need to keep the addresses different ?

HTH
fwyzard



More information about the Gcc-help mailing list