This is an online C++ test question, which has been done.
#include<iostream>
using namespace std;
class A
{
};
class B
{
int i;
};
class C
{
void foo();
};
class D
{
virtual void foo();
};
class E
{
int i ;
virtual void foo();
};
class F
{
int i;
void foo();
};
class G
{
void foo();
int i;
void foo1();
};
class H
{
int i ;
virtual void foo();
virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0;
}
output:
sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo , int : 4
sizeof(class G) after adding foo , unsigned int : 4
sizeof(class g) after adding foo , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16
My questions:
Why siszeof(A) is 1 and sizeof(C) is 1 too ?
Why siszeof(H) is 16 but sizeof(G) is 4 ?
Why siszeof(E) is 16 but sizeof(F) is 4 ?
Why siszeof(D) is 8 but sizeof(E) is 16 ?
My guess:
A virtual function is a pointer with 8 bytes.
But, I do not know why E size is 16 ?
Adding a function to an empty class does not change its size ?
Any help is appreciated.
thanks
First off, a virtual function is not a pointer with 8 bytes. In C++ nothing but
sizeof(char)is guaranteed to be any number of bytes.Second, only the first virtual function in a class increases its size (compiler-dependent, but on most – if not all – it’s like this). All subsequent methods do not. Non-virtual functions do not affect the class’s size.
This happens because a class instance doesn’t hold pointers to methods themselves, but to a virtual function table, which is one per class.
So if you had:
and
you would have
sizeof(A) == sizeof(B).And now:
AandChave size 1 just because it’s not allowed for a class to be of size 0. The functions have nothing to do with it. It’s just a dummy byte.Ghas only one member that accounts for memory – theint. And on your platform,sizeof(int) == 4.H, besides theint, also has a pointer to thevftable(virtual function table, see above). The size of this, size of int and allignment are compiler specific.Explained above – non virtual methods don’t take up memory in the class.
Donly contains thevftablepointer which is apparently 8 bytes on your platform.Ealso has an int, and thevftableis aligned to 8 bytes. So it’s something like: