Reputation: 3086
I know it might sound like a weird question, but I was just wondering if class in C++ weights more than a struct with the same data fields, and there's this one thing I couldn't find an answer for...
Consider this:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8
std::cout<<sizeof(SomeClass)<<std::endl; // output is 8
}
But now see what happens when I add a destructor to SomeClass:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
virtual ~SomeClass(){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8 bytes
std::cout<<sizeof(SomeClass)<<std::endl; // output is 16 bytes!
}
Why does SomeClass need 8 more bytes for the destructor?
Upvotes: 10
Views: 1710
Reputation: 2864
The size increase is because of virtual
. If you don't make the destructor virtual, you won't see the size increase.
So, it's not the destructor that makes your type bigger, but rather it's adding a virtual function that's doing it.
The 8 extra bytes in question is a pointer to the virtual table (vtable) for the class you are using. As noted in the comments, this is a "one time" cost. Adding one virtual function to a class brings on this cost, but you don't see that cost with additional virtual functions.
Edit:
The additional size in the class will depend on whether this is compiled as a 32 bit or 64 bit program. The link to the virtual table takes 4 extra on 32bit, and 8 extra bytes on a 64bit platform.
Upvotes: 18
Reputation: 199
When you declare a destructor as virtual the compiler automatically adds a vtable pointer as a member in your class. This is necessary so it can find the address of all the destructors of the classes derived from yours.
This new member (which you cannot access trivially, neither should you) is added into your class and thus increases its size by the size of a trivial raw pointer, which is usually the same size as an int but may depend on your architecture.
Upvotes: 0
Reputation: 96291
It's not the fact that you added a destructor. It's the fact that you added a virtual method (additional virtual methods would incur no additional per-instance cost). Most (all?) C++ implementations use a virtual function pointer table to enable dynamic dispatch, and this requires a pointer to the appropriate table to be stored within each object. This pointer-to-virtual-table is the extra space per object you're seeing.
Upvotes: 4