Memory layout in multilevel inheritance and empty base optimization

90 views Asked by At

Consider the following inheritance:

struct Empty {};

struct A: Empty {
    int64_t a;
    int8_t b;
};

struct B: A {
    int32_t c;
};

The sizeof of B is 16 on GCC and Clang, but 24 on MSVC. If you remove inheritance A from Empty:

struct A {
    int64_t a;
    int8_t b;
};

struct B: A {
    int32_t c;
};

the sizeof of B suddenly becomes 24 on all 3 compilers. I guess that it is related to empty base optimization, but I can't figure out the actual layout (and the reasoning for such) of these classes in memory in different cases and compilers. Also, I would appreciate references to the standard that say something about EBO in multilevel inheritance.

I tried to look at offsetof(B, c) on GCC and Clang in different inheritance scenarios. In the first case, it's equal to 12 and 16 in the second, but I don't understand why.

0

There are 0 answers