[IRCServices Coding] GCC3

Finny Merrill griever at t2n.org
Mon Feb 25 22:18:47 PST 2002


On Tue, 26 Feb 2002, Andrew Church wrote:

> >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).
Not the largest type in the structure, the largest *type*.
Most structures will pad to 32 bits on intel machines.

like this:

struct {
  int8_t byte;
  /* inserts 8 or 24 bits of padding here */
  int16_t word;
  /* inserts 16 bits of padding here */
  int32_t dword;
  /* no padding here */
} something;

> 
> >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.
> 
IF I remember correctly, POSIX mandates uniform structure padding
for all compilers on a single platform.

>   --Andrew Church
>     achurch at achurch.org
>     http://achurch.org/
> ------------------------------------------------------------------
> To unsubscribe or change your subscription options, visit:
> http://www.ircservices.za.net/mailman/listinfo/ircservices-coding
>