Mail Archives: djgpp/1999/08/15/18:26:36
Chris Holmes wrote:
>
> What exactly does __attribute__ ((packed)) tell the compiler to do?
> It just fixed several problems I was having with some stuff, only it
> doesn't make any sense to me as to why that worked.
Imagine a struct like the following:
struct foo {
char c;
int i;
};
GCC knows that an x86 will get better performance when an `int' is
aligned on a 4-byte boundary; therefore, it inserts three bytes of
padding between `c' and `i'. If your program expects (usually because
of trying to use data written in a file) that `i' immediately follows
`c', it will break. __attribute__((packed)) tells the compiler not to
do this padding, though there is a speed penalty.
It's probably better to write the code in such a way that it's
independent of the way the struct is laid out. For instance, don't try
to read an entire struct from a file directly; read the individual
members and fill them in.
--
Nate Eldredge
nate AT cartsys DOT com
- Raw text -