C structure alignment

Tom Roberts tjr at ihnet.UUCP
Thu Apr 12 01:20:24 AEST 1984


Structure alignment in C varies, depending upon which machine you are on!

Many CPU's cannot reference an int at an odd-byte address, so their
C-compiler ALWAYS locates a structure on an even-byte address. On some
machines (with 4-byte int's), the restriction is to 4-byte addresses.
This applies to structures within structures, even when the structures
only contain char-arrays.

Note that the compilers are permitted to do this, because the only LEGAL
things you can do to a structure are to take its address, reference a
single member, or assign the entire structure to another structure of
the same type (newer C-compilers only). Internal padding can never be
significant for these operations (see the recent discussion of == for
structures).

It is NOT PORTABLE to assume the ordering of members within a structure,
nor to assume they are contiguous in memory. Some machines that are
not byte-addressable may have some big surprises here (the PDP-10
comes to mind). For all byte-addressable machines with which I am familiar,
if you make EVERY member of a structure a multiple of 4 bytes long, data
can be passed between machines in the format of the structure, ASSUMING THE
BYTE-ORDERING IS THE SAME, OR HAS BEEN CORRECTED.

	Tom Roberts
	ihnp4!ihnet!tjr



More information about the Comp.lang.c mailing list