Structure padding

Paul Canniff 2/1011 paulc at microsoft.UUCP
Sun Apr 9 10:22:56 AEST 1989


In article <440 at front.se> zap at front.se (Svante Lindahl) writes:
|Consider a struct consisting of a few character arrays, for example:
|	stuct foo {
|	    char bar[3];
|	    char baz[6];
|	    char frotz[4];
|	} fred;
|I need to know if I can safely assume that there will not be any
|padding between the arrays.

Don't unless you have a real size problem.  But read on for details ...

Microsoft C does indeed have options to enable or disbale padding of
structures to WORD slignment, for faster operations on Intel's processors.
The default is WORD alignment.  The option can be set to BYTE,
DWORD or WORD.  (See /Zp in MSC user's ref).

But ... even in the Intel world, the example above would not be effected
since the arrays are of type char, and will (theoretically) be accessed
a byte at a time.  So the compiler doesn't bother to word-align the
elements.

The moral ... you can probably assume byte alignment, 
since most C compilers will have to aimplement it (at whatever
cost to performance), but make a note of it in your porting instructions!



More information about the Comp.lang.c mailing list