Destructor for Dynamic Array in C++
- Destructor for Dynamic Array in C++
- Need for Destructor in C++
- Examples of Destructor in C++
- Delete Dynamic Array Using Destructor in C++
- Conclusion

While programming in any programming language, we often hear about constructors and destructors. Constructors are special member functions used to initialize the object of a class, whereas destructors are also special functions that are automatically called when an object is going to be destroyed.
This article will discuss the destructor for dynamic arrays in C++ in detail.
Destructor for Dynamic Array in C++
Destructors are special member functions that have the same name as the class and are automatically invoked when an object is going to be destroyed.
The destructor function is the last function that is called when an object is destroyed. It cleans up the memory that the objects created by the constructor occupy.
However, both the constructor and destructor have the same name as that of the class, but there lies a difference between the two in terms of how they are written so that the compiler can differentiate them. The destructors are written using a negation symbol (~
) followed by the class name, and the constructors with just the class name.
Moreover, there can only be a single destructor for a class, but the constructors can be multiple.
Whenever an object is created in C++, it occupies memory in the heap section, and we use the new
keyword to dynamically create that object of the class. Similarly, the delete
keyword is used to delete the dynamically created object of the class.
Need for Destructor in C++
We need to delete the object to avoid any memory leaks. A memory leak occurs when the programmers forget to delete the memory in the heap that is not in use. Because of this, the computer’s performance reduces as it reduces the amount of available memory.
Moreover, if the objects are not explicitly deleted using the destructors, the program will crash during the runtime.
Examples of Destructor in C++
Now, let us see some examples of how to delete the object of a class using the delete
operator.
Example 1:
In the first example, we will be dynamically creating the object of a class using the new
operator and deleting it explicitly using the delete
keyword in C++.
Below is the code example that uses the delete
operator in C++.
#include <iostream>
using namespace std;
class Employee {
public:
Employee()
{
cout << "Constructor of Employee class was called!\n";
}
~Employee()
{
cout << "Destructor of Employee class was called!\n";
}
void work()
{
cout << "Still Working!\n";
}
};
int main()
{
Employee* emp = new Employee();
emp->work();
delete emp;
return 0;
}
Output:
Constructor of Employee class was called!
Still Working!
Destructor of Employee class was called!
In the above example, we have made a class Employee
with a constructor, destructor, and a work
function containing the appropriate message about their actions.
In the main
function, we have created the object of the class emp
using the new
keyword and have called the work()
function with it. After which, we just used the delete
operator and the object name emp
to delete the same.
Delete Dynamic Array Using Destructor in C++
Until now, we have seen how to delete a single object of a class using the delete
keyword in C++. Now, let us see how to delete a dynamic array or an array of objects in C++.
In this example, we will create a dynamic array in C++ using the new
keyword and then delete it using the delete
operator.
However, one thing to remember while deleting a dynamic array in C++ is that we have to use the delete []
operator for doing so and not just delete
. Therefore, the delete
operator is used to delete a single object of a class, whereas to delete a dynamic array or an array of objects, we have to use the delete []
operator in C++.
Now, let us understand it with the help of a code example.
#include <iostream>
using namespace std;
class Employee {
public:
Employee()
{
cout << "Constructor of Employee class was called!\n";
}
~Employee()
{
cout << "Destructor of Employee class was called!\n";
}
void work()
{
cout << "Still working!\n";
}
};
int main()
{
Employee* emp = new Employee[3];
emp[0].work();
emp[1].work();
emp[2].work();
delete[] emp;
return 0;
}
Output:
Constructor of Employee class was called!
Constructor of Employee class was called!
Constructor of Employee class was called!
Still working!
Still working!
Still working!
Destructor of Employee class was called!
Destructor of Employee class was called!
Destructor of Employee class was called!
In this code example, we have a similar code as the previous example, but this time, we have created three Employee
objects and deleted them using the delete []
operator in c++.
However, if we use the delete
operator instead of the delete []
, the program crashes during runtime. Let us also see an example of that to understand the deletion of the dynamic array in C++ more clearly.
#include <iostream>
using namespace std;
class Employee {
public:
Employee()
{
cout << "Constructor of Employee class was called!\n";
}
~Employee()
{
cout << "Destructor of Employee class was called!\n";
}
void work()
{
cout << "Still working!\n";
}
};
int main()
{
Employee* emp = new Employee[3];
emp[0].work();
emp[1].work();
emp[2].work();
delete emp;
return 0;
}
Output:
Error in `./example1.cpp': munmap_chunk(): invalid pointer: 0x000000000117f018 ***
timeout: the monitored command dumped core
/bin/bash: line 1: 33 Aborted timeout 15s ./0f6c05b9-ee09-44c3-acee-8ad8a882ac5e < 0f6c05b9-ee09-44c3-acee-8ad8a882ac5e.in
Constructor of Employee class was called!
Constructor of Employee class was called!
Constructor of Employee class was called!
Still working!
Still working!
Still working!
Destructor of Employee class was called!
Therefore, as you can see in the above output, the constructors and the function work
have been called for all three objects, but the destructor has only been called once for a single object, after which the program crashed.
Conclusion
In this article, we have discussed the destructor for dynamic arrays in C++. The destructors are special member functions that are automatically called when an object is going to be deleted.
We use the delete
operator to delete an object of a class; however, it only deletes a single object of a class. For deleting a dynamic array or an array of objects, we use the delete []
operator.
Both the operators and their usage have been explained with proper examples in this article.
Related Article - C++ Array
- Associative Arrays in C++
- Represent the Deck of Cards in C++ Array
- Deallocate a 2D Array in C++
- Array of Arrays in C++
- Check if an Array Contains an Element in C++