Ordering of elts in structures

Radford Neal radford at calgary.UUCP
Tue Apr 2 07:17:28 AEST 1985


> In a previous news item concerning packet structures I stated that I
> had not seen any condition on the ordering of structure elements in
> the language definition with a caviat of not being sure.
> 
> Hunting this down in the back of K&R the language manual (page 192)
> it does state that structure elements have increasing addresses
> in order of declaration.

This sounds to me like a good candidate for deletion in the ANSI standard
for C. What's the point of specifying order if one doesn't specify alignment?
If the compiler re-orders the fields in decreasing order of alignment 
requirements it would never have to pad to align at all, even on machines 
requiring alignment.

This would seem to address the only legitimate reason for wanting a
"packed" attribute - desire to save space. (As has been pointed out by
others, the idea that you can transfer files between machines using
"packed" is naive.)

Of course, there's a problem with this. Sometimes one wishes to simulate
Simula-style prefixed classes in C, and this requires that one be able
to cheat on the typing in a way which would be screwed up if the compiler
re-arranged the fields. There are two possible solutions to this - add
structure prefixing to C, or add a facility to disable the field
re-arraingment. 

Either is another addition of course. Maybe it all just ought to be left
alone. Sigh...


     Radford Neal
     The University of Calgary



More information about the Comp.lang.c mailing list