Subject: Re: Strange C compiler code generation
To: Charles M. Hannum <root@ihack.net>
From: Richard Earnshaw <rearnsha@buzzard.freeserve.co.uk>
List: port-arm32
Date: 02/25/2001 08:40:32
> 
> On Sun, Feb 25, 2001 at 12:30:06AM +0000, Richard Earnshaw wrote:
> >
> > It turns out that the reason for the difference between NetBSD and linux 
> > is that on linux, structures are, by default, aligned to a 32-bit 
> > boundary, even if they are composed solely of elements with less alignment 
> > than that.  On netbsd, structures are aligned to a byte boundary if no 
> > field within the structure requires a larger alignement.  Hence
> > 
> > #pragma pack(1)
> 
> Are you sure the Lignux compiler isn't just ignored the #pragma?  AFAIK
> it's always been silently ignored on platforms that don't support it.
> 
> 

Yes, in this case, quite sure.  As currently implemented pragma pack packs 
the elements within a structure, it doesn't change the alignment of the 
structure itself; that is, it is equivalent to writing packed against each 
field, rather than against the whole structure:

struct prag_pack
{
  char a; __attribute__((packed));
  int  b; __attribute__((packed));
};

struct really_packed
{
  char a;
  int b;
} __attribute__((packed));

these two differ in behaviour when the STRUCTURE_SIZE_BOUNDARY rule is 
different, the first retains its STRUCTURE_SIZE_BOUNDARY characteristic, 
the second doesn't.  On linux that means that the first remains word 
aligned.  On NetBSD, it becomes byte-aligned.

R.