Deletion of array of objects in C++
Last Updated :
18 Jan, 2021
Need for deletion of the object:
- To avoid memory leak as when an object is created dynamically using new, it occupies memory in the Heap Section.
- If objects are not deleted explicitly then the program will crash during runtime.
Program 1: Create an object of the class which is created dynamically using the new operator and deleting it explicitly using the delete operator:
C++
// C++ program to create an object
// dynamically and delete explicitly
#include <iostream>
using namespace std;
// Class
class Student {
public:
// Constructor
Student()
{
cout << "Constructor is called!\n";
}
// Destructor
~Student()
{
cout << "Destructor is called!\n";
}
// Function to display the message
void write()
{
cout << "Writing!\n";
}
};
// Driver Code
int main()
{
// Create an array of objects
Student* student = new Student();
// Function Call to write()
// using instance
student->write();
// De-allocate the memory
// explicitly
delete student;
return 0;
}
Output:
Constructor is called!
Writing!
Destructor is called!
Program 2: Create an array of objects using the new operator dynamically. Whenever an array of the object of a class is created at runtime then it is the programmer’s responsibility to delete it and avoid a memory leak:
C++
// C++ program to create an array of
// objects and deleting it explicitly
#include <iostream>
using namespace std;
// Class
class Student {
public:
// Constructor
Student()
{
cout << "Constructor is called!\n";
}
// Destructor
~Student()
{
cout << "Destructor is called!\n";
}
// Function to display message
void write()
{
cout << "Writing!\n";
}
};
// Driver Code
int main()
{
// Create an array of the object
// dynamically
Student* student = new Student[3];
// Function Call to write()
student[0].write();
student[1].write();
student[2].write();
// De-allocate the memory
// explicitly
delete[] student;
return 0;
}
Output:
Constructor is called!
Constructor is called!
Constructor is called!
Writing!
Writing!
Writing!
Destructor is called!
Destructor is called!
Destructor is called!
Program 3:
Below is the program where delete is used to delete an array of objects:
C++
// C++ program to delete array of
// objects
#include <iostream>
using namespace std;
// Class
class Student {
public:
// Constructor
Student()
{
cout << "Constructor is called!\n";
}
// Destructor
~Student()
{
cout << "Destructor is called!\n";
}
// Function to display message
void write()
{
cout << "Writing!\n";
}
};
// Driver Code
int main()
{
// Create an object dynamically
Student* student = new Student[3];
// Function call to write()
student[0].write();
student[1].write();
student[2].write();
// De-allocate the memory
// explicitly
delete student;
return 0;
}
Explanation: This program will crash in runtime. In this program, constructors are working properly but the destructor is executed only for the first object, and after that program is crashing at runtime because of a memory leak. It is because 3 objects are created at runtime but only one object is deleted explicitly and that's why the remaining two objects are crashing at runtime.
Conclusion:
In C++, the single object of the class which is created at runtime using a new operator is deleted by using the delete operator, while the array of objects is deleted using the delete[] operator so that it cannot lead to a memory leak.
Similar Reads
How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of
2 min read
Life cycle of Objects in C++ with Example In Object Oriented Programming, Objects are the instances of a class which has its own state(variables) and behavior(methods). Every class has two special methods related with creation and destruction of object of a class- constructors and destructors. C++ Object Life Cycle: There are various steps
6 min read
How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
2 min read
How to Create a Map of Arrays in C++? In C++, the std::map is a container that stores elements in a key-value pair, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a map of arrays in C++. Example: Input: arr1 = {1, 2, 3};arr2 = {4, 5, 6};arr3 = {7, 8, 9};
2 min read
How to Delete a Pointer in C++? In C++, memory management is essential to avoid memory leaks, crashes, and undefinable behavior. In this article, we will learn how to properly delete a pointer in C++. Deleting a Pointer in C++By deleting a pointer, we refer to deleting the memory allocated to the pointer. To delete a pointer in C+
2 min read