Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi ,
what is the size of empty class in c++ and if the class contain only one VTABLE then what is the size?


Please explain?

Regards,
Ranjith
Posted
Comments
Philippe Mori 25-Dec-12 9:45am    
Why not try it first or use Google?

For example, a better question would be why on my compiler an empty class is not really empty... At least, it would show us that you have tried something.

In c++ sizeof operator will never returns zero. so i can say it will be a non zero which depends on the compiler your are using.

jkchan
http://cgmath.blogspot.com/[^]
 
Share this answer
 
Comments
Philippe Mori 25-Dec-12 9:40am    
It is not that the operator result cannot be 0. It is that the class cannot be empty and still be conformant to C++ spécifications (each time you call new, it should give a different address provide that previous addresses are still in use).
ranjithkumar81 26-Dec-12 0:20am    
ok,

class Test
{
public:
virtual void function()
{
}
};

what is the size of Test class in 32bit machine and 64bit machine?
Your time would be much better spent studying the language in detail rather than randomly posting questions here. You could start by reading everything on Bjarne Stroustrup's website[^].
 
Share this answer
 
Is is non-zero, you may see the reason, for instance, here: "Empty Classes" at MSDN[^].
 
Share this answer
 
Comments
ranjithkumar81 24-Dec-12 3:36am    
#include<iostream>


using namespace std;

class empty{
public:

virtual void function()
{
}

};

int main()
{

printf ("Size of empty is= %d\n", sizeof(empty));
return 0;
}

I am getting size of empty class is one byte and in a class contain one virtual function ,the size is 8 bytes.how?
any one can explain?
Philippe Mori 25-Dec-12 9:37am    
At least one pointer is required for the vtable. There are also alignment issues.

The actual size depend on the compiler options and the platforms.
CPallini 24-Dec-12 4:21am    
You get 8 because, when you add the virtual function, a vtable for the class must be created (and each instance of the class must hold a pointer to such vtable, and your machine is possibly a 64 bit one).
ranjithkumar81 24-Dec-12 5:33am    
My question is not related to 64 bit machine and 32 bit machine?

for example:

class Base{
public:
int a;
};


void main()
{
printf("Size of class %d",sizeof(Base));

}

its print 4. because int ocoupy 4 bytes in memory likewise ,i am asking about the VTABLE.
CPallini 24-Dec-12 5:43am    
Your test just tells us pointer size is 64 bits on your (64 bit) machine while int size is 32 bits.
C++
int main()
{
    class empty {};
    printf ("Size of empty is %d", sizeof(empty));
    return 0;
}

About VTable, virtual destructors and similar - google is your friend.

Hope this helps,
Pablo.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900