Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

destructor

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?

like image 226
so.very.tired Avatar asked Jul 11 '14 18:07

so.very.tired


People also ask

Does destructor destroy the object?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

Can destructors be overloaded?

It is not possible to define more than one destructor. The destructor is only one way to destroy the object create by constructor. Hence destructor can-not be overloaded. Destructor neither requires any argument nor returns any value.

Do destructors return a value?

Declaring destructorsDo not return a value (or void ). Cannot be declared as const , volatile , or static . However, they can be invoked for the destruction of objects declared as const , volatile , or static .

Why destructor is used when delete is there?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.


1 Answers

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.

like image 164
Michael Gazonda Avatar answered Sep 21 '22 06:09

Michael Gazonda