[IRCServices Coding] GCC3

Finny Merrill griever at t2n.org
Mon Feb 25 23:43:26 PST 2002


On Tue, 26 Feb 2002, Andrew Church wrote:

> >>      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;
> 
>      That's missing the point; you put a 32-bit type in there, which of
> course means it will pad to 32 bits.  (And by your argument, it would have
> to pad to at least the size of a double, not just an int32_t.)  What you're
> saying would be something like:
> 
> struct {
>     int8_t byte;
>     /* 24 bits of padding */
>     int16_t word;
>     /* 16 bits of padding */
> } foo;  /* size = 64 bits */
> 
> which is stupid because you have 32 bits of wasted space, when you could
> just as easily and with no alignment problems (at least on any CPU I know
> of) have done:
> 
> struct {
>     int8_t byte;
>     /* 8 bits of padding */
>     int16_t word;
> } bar;  /* size = 32 bits */

Hmm, you're right. BUT, some compilers might be too stupid to do
it this correct way.
Plus if you did this:

struct {
  int8_t byte;
  /* 8 bits of padding */
  int16_t word1, word2;
  /* 16 bits of padding! */
} bar;

it pads the extra 16 bits so it's on a 32 bit boundary.

and btw, even if you have doubles or long longs in there, it still
aligns on 32 bit boundaries.
> 
>   --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
>