Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++Windows
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?
Posted 23-Dec-12 20:25pm
Philippe Mori at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Is is non-zero, you may see the reason, for instance, here: "Empty Classes" at MSDN[^].
ranjithkumar81 at 24-Dec-12 3:36am
#include 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 any one can explain?
Philippe Mori at 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 at 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 at 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 at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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[^].
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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.
Philippe Mori at 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 at 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?

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

  Print Answers RSS
Your Filters
0 Shai Vashdi 1,448
1 Tadit Dash 270
2 OriginalGriff 223
3 Sergey Alexandrovich Kryukov 215
4 Peter Leow 180
0 Sergey Alexandrovich Kryukov 9,440
1 OriginalGriff 5,618
2 Peter Leow 4,280
3 Maciej Los 3,540
4 Abhinav S 3,363

Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid