I've noticed some rather annoying data alignment behavior with Microsoft C++ compiling for X64. Perhaps it's in the C++ standard but I don't see the logic behind it. Here's the situation.
If I have a class, call it A, with a Vtable and a 4 byte integer, then sizeof(A) returns 16 which is what I would expect because of 8 byte alignment. Now if I add another four byte integer to class A, sizeof(A) still returns 16, which is again what I would expect. However if instead of adding the second four byte integer to A, I create a new class B which inherits from A and then I add the second four byte integer to B, sizeof(B) now returns 24. This I don't get. I mean I understand the initial 8 byte alignment, but since you can't possibly have a B without an A, I don't see why the start of B also has to also be aligned on 8 bytes. It's just wasted space. It means that sub-classing can now incur a space penalty for no apparent reason. Anyone have an explanation for this?
I also tested this making the class non virtual, with simply a double instead, just to see if the Vtable was causing something odd to happen. However it exhibited the exact same behavior so it seems to be some standard data alignment thing.