Why do destructors in C++ increase the size of the object they're in?

c++, destructor

Solution

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.

Problem

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?

Original source

Related problems