Can someone explain to me what is going on here? Firstly, I think most programmers know that a class with a virtual function has vtbl and, therefore, has 4 extra bytes on top. As far as I know, this is pretty standard. I tested this and took advantage of this fact before doing a load on a place from a binary file with fixed vtbls. Over the past 6 months, I have been working in Xcode and most recently faced the need to do some downloads in place, so I studied the vtbls fixes again. To make sure my understanding is correct, I wrote a sample program. Here he is:
class A { public: virtual int getData() { return a; } virtual void print() { printf("hello\n"); } int a; }; class B : public A { public: int getData() { return b; } int b; }; class C : public B { public: int getData() { return c; } void print() { printf("world\n"); } int c; }; class D { public: int a; int b; }; int main (int argc, const char * argv[]) { A* tA = new A(); tA->a = 1; printf("A: %d\n", sizeof(A)); printf("A data: %d\n", tA->getData()); B* tB = new B(); tB->a = 2; tB->b = 4; printf("B: %d\n", sizeof(B)); printf("B data: %d\n", tB->getData()); C* tC = new C(); tC->c = 8; printf("C: %d\n", sizeof(C)); printf("C data: %d\n", tC->getData()); A* aC = tC; aC->print(); printf("D: %d\n", sizeof(D)); return 0; }
My expected result:
A: 8
Data: 1
B: 12
Data B: 4
C: 16
Data C: 8
world
D: 8
However, the conclusion I get is:
A: 16
Data: 1
B: 16
Data B: 4
C: 24
Data C: 8
world
D: 8
Does anyone know what is going on here? Thanks!
source share