C structure alignment

tll at druxu.UUCP tll at druxu.UUCP
Thu Apr 12 11:59:30 AEST 1984


>From: tjr at ihnet.UUCP (Tom Roberts)
>
>It is NOT PORTABLE to assume the ordering of members within a structure,
>nor to assume they are contiguous in memory.

It's true that members may not be contiguous in memory, but (time for
the bible-thumping quote from The C Reference Manual, p196)

	"Within a structure, the objects declared have addresses which
	increase as their declarations are read left-to-right."

So the ordering is clearly defined.  This feature is useful when you
need to store a large number of structures that have differing amounts
of data in them (for instance, in a circuit simulator, you might want a
structure for each component -- early in the structure, you define what
kind of component this is, and the rest of the structure is optimized
for the particular component).  A union could be used, but then you use
the maximum amount of storage for each structure, which may be more than
you have.

		Tom Laidig
		AT&T Information Systems Laboratories, Denver
		...!ihnp4!druxu!tll



More information about the Comp.lang.c mailing list