This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: How to stop gcc padding structs???



>   I'm again fighting with gcc trying (and failing) to get it to
>   stop putting padding bytes into structs.  Has anybody figured
>   out how to prevent gcc from padding structs?
>   
>   I ran into this problem before and gave up, finally having to
>   use hand-calculated constants instead of "sizeof (struct foo)"
>   in numerous places.  For example, it's impossible to define an
>   Ethernet header structure that ends up having a size of 14
>   bytes!
> 
> You have to change the alignment of the types that you're using.
> The size of an object of a type is always a multiple of its alignment.
> Probably you're using types like "int" with an alignment of 4 bytes,

No, I was using a struct containing two "char" variables.  Each
of those structs takes up 4 bytes, even though the two char
variables are packed into two bytes.

typedef struct 
{
  char x __attribute__((packed));
  char y __attribute__((packed));
}foo __attribute__((packed));

sizeof (bar) is 4.  I want it to be 2.

> which will cause your entire structure to have a 4 byte
> alignment.
> 
> Be aware that unaligned types may be very inefficient on
> certain machines, as a variable access might need two reads and
> some shift/and/or glue.

Unaligned types suck, but that's the way the hardware was
designed and there's nothing I can do about it other than try
to convince gcc to access the right addresses.

-- 
Grant Edwards
grante@visi.com

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]