Compilers typically insert lazy padding on structures to satisfy alignment constraints when used in arrays:
Now consider a combination of them using the internal structure:
struct AB { struct { uint16_t x; uint8_t y; } xy; uint8_t z; };
The following can fit in 4 bytes without violating alignment restrictions. In addition, xy internal structure does not have a type that could be used elsewhere, so it does not require scrolling to scroll.
The disadvantage is that the xy member will not be compatible with struct A , but there is no reason that it should be the same as it is in the definitions of different types.
Is the compiler allowed to optimize this size?
Or, in other words, does the standard require that 2 structures with equal members always result in equal distribution?
source share