0

This is a follow-up question to the one I posted yesterday:

sizeof(myobject) not what I have calculated

I create a class and calculate its size using sizeof(myclass). The size is 12 bytes. I then add virtual members, and the size goes up to 24 bytes. It appears to be the data packing problem again (since the size of the vtable pointer is only 8 bytes).

So, my quesion is: is it possible to tell the compiler to put the vtable pointer before the other data members so that I can keep that extra 4 bytes of unused space? I know I can order the data members, but can I "order" the vtable pointer?

John

Community
  • 1
  • 1
  • You should mention your compiler, compiler version, and OS when making posts like this. – M.M Jun 10 '14 at 02:51

1 Answers1

0

Ordering likely isn't the problem, your vtable pointer probably must be 8 byte aligned even if you have an array. If the size was 20, every second element in an array would have a vtable pointer which was misaligned by 4 bytes.

(Mind you, it's not the content of the vtable pointer which would be misaligned. In an array, all vtable pointers are equal)

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yes, but shouldn't the compiler put the vtable pointer first in the array in order to avoid misalignment? – user3335011 Jun 09 '14 at 23:26
  • @user3335011: If the alignment requirement for the pointer is 8, then the size of the class must necessarily be a multiple of 8. (Think `T[N]`.) – Kerrek SB Jun 09 '14 at 23:36