top | item 43422233

(no title)

mmaniac | 11 months ago

Isn't struct layout in C implementation defined in general?

C itself doesn't specify any ABI. A given platform simply uses one as a matter of convention.

discuss

order

unwind|11 months ago

No, it's quite well-defined as far as I know. Fields must be packed in the order they are declared, and be suitably aligned, basically.

mmaniac|11 months ago

Time to put my language lawyering cap on.

C99 §6.7.2.1.13

> Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning

C99 §6.7.2.1.13

> An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

Which is standardese for pretty much exactly everything you said :)

The consequence of the first rule is that there's only one sane way to lay out structs. The only way to break that rule which I can imagine would be to add extra padding - you can't swap the order of any members under these rules.