Reputation: 1
Case 1:
class A {
private:
uint32_t a1 : 8;
};
class B : public A {
private:
uint32_t a2 : 2;
uint32_t a3 : 22;
};
size(B) prints 4 Bytes
Case 2:
class A {
private:
uint32_t a1 : 8;
uint32_t a2 : 2;
};
class B : public A {
private:
uint32_t a3 : 22;
};
size(B) prints 8 Bytes
So Why these two cases corresponding to 2 different results?
Upvotes: 0
Views: 65
Reputation: 162164
So Why these two cases corresponding to 2 different results?
Because the compiler is allowed to place struct/class members at any offset it sees fit, as long as the order of members is preserved. And while it's somewhat "predictable" how things are padded and where they end up for "normal" types, any notion of "rules of thumb" go out of the window as soon as bitfields and inheritance are mixed.
Upvotes: 1