Pragma packing and effect on inheritance ? Packing is not lifted eventhough specified?

499 views Asked by At

Assume:

  • Class A (base class) with 4 bytes to spare but the size is 80 bytes due to 8-byte alignment

  • Class B that inherits from A. Only adds 4 bytes to the object but its size is 88 bytes due to 8-byte alignment

  • Class C that inherits from B. Only adds 4 bytes to the object but its size is 96 bytes due to 8-byte alignment

I figured, why no pack class A and gain 8 bytes on all C class objects (and objects that inherit from class C)

So I put #pragma pack(push, 4) / #pragma pack(pop) around class A. NO packing around B and C !

Object A's size reduced from 80 bytes to 76 bytes.

Object B's size is now 80 bytes. It coincides with QWORD alignment which is perfect.

I expected object C to be 88 bytes, BUT it turns out to be 84 bytes ... so the 4 byte alignment is still in effect? Even if I use #pragma pack(push, 8) to force it back to 8 byte alignment, it doesn't seem to work.

I thought I had found a way to reduce some 'middle' objects in size and was carefully plotting out which objects to pack and which not, to make sure the most used 'end' objects would nicely be 8 byte aligned and its 64 bit variables would be on 8 byte alignment etc. However it doesn't seem to work that way.

Is this normal behaviour or is this due to my compiler (Borland / CodeGear / Embarcadero C++ Builder 2009) ?

0

There are 0 answers