Posts

Showing posts from March 26, 2019

Standard-layout and tail padding

Image
18 3 David Hollman recently tweeted the following example (which I've slightly reduced): struct FooBeforeBase { double d; bool b[4]; }; struct FooBefore : FooBeforeBase { float value; }; static_assert(sizeof(FooBefore) > 16); //---------------------------------------------------- struct FooAfterBase { protected: double d; public: bool b[4]; }; struct FooAfter : FooAfterBase { float value; }; static_assert(sizeof(FooAfter) == 16); You can examine the layout in clang on godbolt and see that the reason the size changed is that in FooBefore , the member value is placed at offset 16 (maintaining a full alignment of 8 from FooBeforeBase ) whereas in FooAfter , the member value is placed at offset 12 (effectively using FooAfterBase 's tail-padding). It is clea