Is the address of the first data member of an instance the same as the address of the instance? [duplicate]

  c++, class, memory-alignment
class Foo {
    public:
        int a;
        int b;
};

Foo x;
bool sameAddress =  (void*)&x  ==  (void*)&(x.a);

I have not yet come across a compiler/architecture for which sameAddress is false, but I have only looked at about 6 different variants. Is this defined or undefined? Does it depend on whether std::is_standard_layout<Foo>::value is true (and/or other aspects of the class definition)? Does #pragma pack( push, 1 ) help in guaranteeing it?

Source: Windows Questions C++

LEAVE A COMMENT