This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: How to stop gcc padding structs???
- To: Geert Bosch <bosch at gnat dot com>
- Subject: Re: How to stop gcc padding structs???
- From: Grant Edwards <grante at visi dot com>
- Date: Sun, 28 Jan 2001 17:09:02 -0600
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- References: <20010128225817.7B25834D80@nile.gnat.com>
> 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