I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10):
6.7.2.1 Structure and union specifiers
[...]
Semantics
[...]
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.
The emphasized sentence stretches my English skills to the limit: I don't understand if it refers to individual bit-fields inside a unit, or to bits ordering inside the individual bit-fields or something else.
I'll try to make my doubt clearer with an example. Let's assume that unsigned ints are 16 bits, that the implementation chooses an unsigned int as the addressable storage unit (and that bytes are 8 bits wide), and no other alignment or padding issues arise:
struct Foo {
unsigned int x : 8;
unsigned int y : 8;
};
thus, assuming x
and y
fields are stored inside the same unit, what is implementation-defined according to that sentence? As I understand it, it means that inside that unsigned int unit, x
can be stored either at a lower address than y
or vice-versa, but I'm not sure, since intuitively I'd think that if no bit fields overlaps with two underlying storage units, the declaration order would impose the same ordering for the underlying bit-fields.
Note: I fear I'm missing some terminology subtlety here (or, worse, some technical one), but I couldn't understand which.
Any pointer appreciated. Thanks!
unsigned x : 1
, if the lowest bit or the highest bit. So ifsizeof(unsigned int) == 4
,x
could be saved in the bit 1 or in the bit 32. – Voe