Destructor for Dynamic Array in C++ Last Updated : 14 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Dynamic memory allocation in C++ allows us to allocate memory during program execution using the pointers. This memory is wholly managed by the programmer from allocation to deallocation. So, when we declare a dynamic member in a class, it is our responsibility to deallocate the memory for that class when the object is destroyed. In this article, we will learn how to implement destructors for dynamic arrays in C++. Deleting Dynamic Array using Class Destructor in C++When working with dynamic arrays in C++, it's essential to create a destructor for classes that deallocates the memory allocated to the dynamic arrays. In this destructor, we can use the delete [] operator to allocate memory for dynamic arrays. Syntaxdelete[] dynamic_Array;The allocated heap memory is not deallocated automatically by the compiler and it must be explicitly deallocated by the user through destructors. The delete[] operator deallocates the entire memory that was allocated for the dynamic array into the heap. C++ Program to Deleting Dynamic Array using Class DestructorThe following program illustrates how we can implement a destructor for dynamic arrays in C++: C++ // C++ Program to Implement Destructor for Dynamic Arrays #include <iostream> using namespace std; // dynamic array class class DynamicArray { private: // pointer to the heap memory int* arr; int size; public: DynamicArray(int s) { size = s; // allocating memory arr = new int[size]; } // Destructor to deallocate memory for the dynamic array ~DynamicArray() { cout << "Memory for the Dynamic Array Released" << endl; delete[] arr; } // Function to add elements to the array void addElement(int index, int value) { if (index >= 0 && index < size) { arr[index] = value; } } // Function to print the elements of the array void printArray() { cout << "Array elements:" << endl; for (int i = 0; i < size; ++i) { cout << arr[i] << " "; } cout << endl; } // some other functions can go here when needed }; int main() { // Create a dynamic array object DynamicArray dynArray(10); // Add some elements to the dynamic array for (int i = 0; i < 10; ++i) { dynArray.addElement(i, i * 2); } // Print the elements of the dynamic array dynArray.printArray(); // The destructor will be called automatically when the // object goes out of scope and the memory will be freed return 0; } OutputArray elements: 0 2 4 6 8 10 12 14 16 18 Memory for the Dynamic Array Released Time Complexity: O(1) as deallocation does not depend on the size of the memory.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Deletion of array of objects in C++ S shivanshmahajan876 Follow Improve Article Tags : C++ Programs C++ cpp-destructor C++-Destructors CPP Examples +1 More Practice Tags : CPP Similar Reads How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy 2 min read How to Initialize a Dynamic Array in C++? In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time o 1 min read How to Dynamically Resize a C++ Array? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to dynamically resize an array in C++. Example: Input: myArray = {10, 20, 30, 40, 50};Output:Resized array: 10 20 30 40 50 0 0 0 0 0 Resizing a Dynamic Array in C++ 3 min read 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 Deletion of array of objects in C++ 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 usi 3 min read How to Dynamically Allocate an Array in C++? In C++, dynamic memory allocation allows us to allocate memory during runtime. Dynamic allocation in an array is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to dynamically allocate an array in 2 min read Like