struct members memory layout

This is true for a struct, but changes in C++ as soon as you introduce access specifiers. The compiler is allowed to reorder whole blocks delimited by access specifiers.


Yes, in C at least. The compiler is free to insert padding after any structure member but it must not reorder the members.

It must also not insert padding before the first member.

From 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.

15/ There may be unnamed padding at the end of a structure or union.

Tags:

C++

C