Mail Archives: djgpp/1995/01/28/21:05:59
On Fri, 27 Jan 1995, Jason Hoffoss wrote:
> Then, I put in a debugging line like:
>
> printf("%d\n", sizeof(header));
>
> Which gave me 24 for output. If you look carefully, however, you will see
That's not a bug. Nothing in the C standard requires structs to
be layed out in a particular way. Code that depends on the exact
layout of a structure is inherently non-portable. GCC aligns
elements of a struct as it sees fit, for efficiency. If you want a
struct to be packed (i.e. no space in-between elements), you must
use the __attribute__ ((packed)) notation on each element of the
structure (see the info pages).
That is a subtle gotcha, though...... I don't know if it's possible to
get gcc to issue a warning in cases like this.
Gordon.
- Raw text -