Optimal structure field ordering

Tim Iverson iverson at cory.Berkeley.EDU
Fri Jul 1 07:43:43 AEST 1988


Many of you have mentioned that allowing the compiler to reorder the
structure would go contrary to the K&R gospel, some even pointed out a few
problems this would cause.  What I would like to point out is the problem
caused already by following the K&R dictum that fields shall be aligned
according to type.

Forced alignment is fine when you're creating your own data, but when you
have to interface directly with hardware or massage foreign binary data into
local format, it can force the programmer to abandon the nice bit field and
structure abilities of C and resort to bit masks and character arrays to
properly address the data.

Fortunately, some C compilers have an option to compile with "packed"
structures.  A new "packed" keyword would be even better, as this would
allow the speed of word aligned structs in all but the instances where it is
necesary to hand-align the data.  As it stands, if you want to pack one
struct, then you must pack them all, or none of the "non-packed" modules
will be able to properly access the structs in the "packed" module.
Hopefully, this feature will be added in the next generation of C, if there
ever will be such a beast.


- Tim Iverson
  iverson at cory.Berkeley.EDU
  ucbvax!cory!iverson



More information about the Comp.lang.c mailing list