Is the compiler free to reorder data in a non-standard layout class? For example, is it allowed to change
struct
{
char x;
private:
short y;
public:
char z;
};
to
struct
{
private:
short y;
public:
char x;
char z;
};
EDIT: I misremembered the quote originally, the compiler is allowed to do that per 9.2/14
:
Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1)
What it can't do is reorder attributes with the same access specifier.
That said I don't know of any implementation that takes the liberty to reorder attributes even across access specifiers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With