Destructors: cleaning up resources, memory management in C++

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
In C++, a destructor is a special member function of a class that is called automatically when an object of that class is destroyed. The destructor is used to clean up any resources that were allocated by the object during its lifetime, such as memory or file handles.
The syntax for declaring a destructor in C++ is similar to that of a constructor, but with a tilde (
In C++, a destructor is a special member function of a class that is called automatically when an object of that class is destroyed. The destructor is used to clean up any resources that were allocated by the object during its lifetime, such as memory or file handles. The syntax for declaring a destructor in C++ is similar to that of a constructor, but with a tilde (
In C++, a destructor is a special member function of a class that is called automatically when an object of that class is destroyed. The destructor is used to clean up any resources that were allocated by the object during its lifetime, such as memory or file handles.

The syntax for declaring a destructor in C++ is similar to that of a constructor, but with a tilde (

) symbol in front of the class name:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class MyClass {
public:
// constructor
MyClass();
// destructor
class MyClass { public: // constructor MyClass(); // destructor
class MyClass {
public:
    // constructor
    MyClass();

    // destructor
    

MyClass();
};

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The destructor is called automatically when the object goes out of scope or is explicitly destroyed using the `delete` keyword. For example:
The destructor is called automatically when the object goes out of scope or is explicitly destroyed using the `delete` keyword. For example:
The destructor is called automatically when the object goes out of scope or is explicitly destroyed using the `delete` keyword. For example:

{
MyClass obj; // create an object of MyClass
} // obj goes out of scope and its destructor is called

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter

{
MyClass* ptr = new MyClass(); // create an object of MyClass dynamically
delete ptr; // explicitly call the destructor and deallocate the object
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
When a class owns dynamic resources, such as memory allocated with `new`, it's important to properly clean up those resources in the destructor. Failure to do so can lead to memory leaks and other resource leaks.
Here's an example of a class that dynamically allocates memory in its constructor and deallocates it in its destructor:
When a class owns dynamic resources, such as memory allocated with `new`, it's important to properly clean up those resources in the destructor. Failure to do so can lead to memory leaks and other resource leaks. Here's an example of a class that dynamically allocates memory in its constructor and deallocates it in its destructor:
When a class owns dynamic resources, such as memory allocated with `new`, it's important to properly clean up those resources in the destructor. Failure to do so can lead to memory leaks and other resource leaks.

Here's an example of a class that dynamically allocates memory in its constructor and deallocates it in its destructor:

class MyArray {
public:
MyArray(int size) {
m_data = new int[size];
m_size = size;
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MyArray() {
delete[] m_data;
}
private:
int* m_data;
int m_size;
};
MyArray() { delete[] m_data; } private: int* m_data; int m_size; };
MyArray() {
        delete[] m_data;
    }

private:
    int* m_data;
    int m_size;
};

In this example, the constructor allocates an array of integers using `new`, and the destructor deallocates the array using `delete[]`. This ensures that the memory is properly deallocated when the object is destroyed, whether it goes out of scope or is explicitly deleted.

It’s worth noting that C++11 introduced a new feature called move semantics, which can be used to transfer the ownership of resources between objects without copying them. This can be useful for optimizing resource management and reducing the overhead of copying large objects. However, it’s still important to properly clean up any resources that an object owns in its destructor, even when using move semantics.