[IRCServices Coding] GCC3

Andrew Church achurch at achurch.org
Tue Feb 26 14:48:57 PST 2002


>Reordering is not permitted by the ANSI/ISO C standards.

     That's what I thought, but a whole bunch of people seemed to think GCC
3.0 was doing just that.

>> struct foo
>> {
>>     int_16 bar;
>>     int_8 baz;
>> };
>>
>> The compiler would append or prepend (depending on compiler)
>> on an extra 40bits of data to align it in memory on each
>> allocation.
>
>Under 32bit x86, it's likely to add 8 bits of padding to the end
>of the structure.  The alignment is for the width of the largest
>datatype.

     Again, that's what I thought--compilers aren't supposed to pad more
than the largest type in the structure, and between structure members only
enough to align the next member to a multiple of its size.  I'm pretty sure
this is defined somewhere, and if not then it should be (see below).

>Partly for this reason, mapping structs onto arbitrary data in
>memory results in undefined behavior.

     It shouldn't, and if it did things would break all over the place.
Suppose you have two compilers, one of which is used to compile a program,
and the other of which is used to compile a library used by the program.
Now suppose the program passes a pointer to a structure (say, a FILE *) to
the library.  If the two compilers use different algorithms to pad
structure members, guess what happens?  Boom.

  --Andrew Church
    achurch at achurch.org
    http://achurch.org/