C structure alignment

Tim Maroney tim at unc.UUCP
Thu Apr 12 12:51:42 AEST 1984


Excuse me, but you CAN assume that members of a struct appear in the listed
order; furthermore, if two structs have a common beginning (that is, their
first n members are identical), you can assume that they will be at the same
offset.  My favorite use of this is writing generic linked list manipulation
functions, in which each cell starts with a pointer to the next, and the
rest of the cell is a value of arbitrary type.  I don't have my K&R handy,
but it's in there.

There was a discussion about this a few months ago.  For a lot of
applications, you don't care about the ordering of struct members, and you'd
like the compiler to rearrange them for maximum storage efficiency within
its alignment requirements.  Unfortunately, you can't do that in C.  (It
would be easy enough to add -- just put a new keyword before the open brace
to signal that you don't care about the order -- but then again, what
wouldn't?)
--
Tim Maroney, The Censored Hacker
mcnc!unc!tim (USENET), tim.unc at csnet-relay (ARPA)

All opinions expressed herein are completely my own, so don't go assuming
that anyone else at UNC feels the same way.



More information about the Comp.lang.c mailing list