Delete Operator in C++
Overview
We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must manually deallocate the dynamically allocated memory. Using the delete operators, we may dynamically deallocate memory.
Memory Management : An Overview
The new and delete operators in C++ provide dynamic memory (that is, the allocation of memory or at runtime by the programmer) and object allocation and deallocation.
When you no longer need to utilise a dynamically defined variable in C++, you can deallocate the memory used by the variable by using the "delete" operator.
You may use the delete operators to allocate memory to the array at runtime, which is one of the applications or uses of dynamic memory allocation in data structures.
Delete Operator in C++ :
Because the coder has allotted space at runtime, it is the programmer's responsibility to destroy that memory when it is no longer needed. When programmers decide that a dynamically created variable is no longer needed, they can use the "delete" operator to free up the memory it takes in the free store or heap. The memory is returned to the operating system. Memory deallocation is another name for this. Additionally, after the programme is finished, memory will be automatically deallocated.
Syntax :
For deallocating memory for only one element.
delete pointer_var;
For deallocating memory for an array.
delete [] pointer_var;
In the aforementioned syntaxes, memory that was pointed by pointer_var has been removed.
For example :
delete scalar
The memory indicated by the variable 'scalar' will be deallocated in the provided example.
It's beneficial to employ dynamic memory allocation for flexibility and other purposes, however if the programmer fails to deallocate memory, a memory leak might occur (in which memory is not deallocated until the programme terminates). For good coding practise and to minimise memory leaks, programmers should explicitly erase the dynamically allocated memory.
Why is delete operator required ?
We all know that when a programme exits or shuts down, variables are automatically deallocated/deleted, but it's important to use the delete operator in order to reuse the memory allocated in the programme if necessary and avoid situations like memory not being left or allocation of a space having an unknown value.
Using the delete() method to delete an array object :
In C++, the delele [] operator may be used to efficiently delete array objects. We'll show you how to remove arrays in C++ in the code below.
Example :
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Allocating the Heap memory
int* arr = new int[7];
// Deallocating the Heap memory
delete[] arr;
cout << "Array Deleted." << endl;
return 0;
}
Output :
Array Deleted.
Explanation :
In the above example, we created a new array called arr first, using new keyword to allocate the heap memory. Then we used the delete[] method to deallocate the heap memory and erase the data. Thus, we got our required output.
Using the delete() method to delete NULL pointers :
The pointer to the object or variable is not removed during the delete operation on pointers. As a result, the delete operation clears the RAM memory region to which the pointer corresponds.
Example :
#include <bits/stdc++.h>
using namespace std;
int main()
{
// pntr is the given NULL pointer
int* pntr = NULL;
// deleting the pntr
delete pntr;
cout<<"NULL pointer Deleted"<<endl;
return 0;
}
Output :
NULL pointer Deleted
Explanation :
In the above example, a NULL pointer was given as a Pointer which was allocated in the memory. Using the delete method we deallocated that NULL pointer from the memory successfully. Thus, the memory held by the NULL pointer variable has been deleted.
Using the delete operator to delete a void pointer :
The delete operator may be used to efficiently clear up memory space taken up by the void pointer variable.
Example :
#include <bits/stdc++.h>
using namespace std;
int main()
{
void* strng;
delete strng;
cout<<"Void Pointer Deleted"<<endl;
return 0;
}
Output :
Void Pointer Deleted
Explanation :
In the above example, a void pointer named strng was allocated to the memory. Then we used the delete operator to deallocate the void pointer strng from the memory.
Using the delete() method to erase dynamically allocated memory :
The delete operator can be used to erase memory that has been dynamically allocated to the pointer variable using the malloc() method.
Example :
#include <bits/stdc++.h>
using namespace std;
int main()
{
char* strng = (char*)malloc(sizeof(char));
delete strng;
cout<<"Pointer memory allocated dynamically is Deleted"<<endl;
return 0;
}
Output :
Pointer memory allocated dynamically is Deleted
Explanation :
In the above example, the pointer variable strng was allocated memory dynamically. Then the delete operator was used to erase memory that has been dynamically allocated to the pointer variable using the malloc() method.