A delete operator is used to deallocate memory space that is dynamically created using the new operator, calloc and malloc function, etc., at the run time of a program in C++ language. In other words, a delete operator is used to release array and non-array (pointer) objects from the heap, which the new operator dynamically allocates to put variables on heap memory. We can use either the delete operator or delete operator in our program to delete the deallocated space. A delete operator has a void return type, and hence, it does not return a value.
Syntax of delete operator
We can delete a specific element or variable using the delete operator, as shown:
delete pointer_variable;
// delete ptr; It deallocates memory for one element
Similarly, we can delete the block of allocated memory space using the delete operator.
delete [ ] pointer_variable;
// delete [] ptr; It deallocate for an array
Program to deallocate memory location of each variable using the delete operator
Let's consider an example to delete the allocated memory space of each variable from the heap memory using the delete operator.
Program1.cpp
#include <iostream>
using namespace std;
int main ()
{
// declaration of variables
int *ptr1, *ptr2, sum;
// allocated memory space using new operator
ptr1 = new int;
ptr2 = new int;
cout << " Enter first number: ";
cin >> *ptr1;
cout << " Enter second number: ";
cin >> *ptr2;
sum = *ptr1 + *ptr2;
cout << " Sum of pointer variables = " << sum;
// delete pointer variables
delete ptr1;
delete ptr2;
return 0;
}
Output
Enter first number: 5
Enter second number: 8
Sum of pointer variables = 13
Program to delete the array object using the delete operator
Let's create a program to delete the dynamically created memory space for an array object using the delete operator in C++.
Program2.cpp
#include <iostream>
using namespace std;
int main ()
{
// declaration of variables
int *arr, max_num, i;
cout << " Enter total number of elements to be entered : ";
cin >>max_num;
// use new operator to declare array memory at run time
arr = new int [max_num];
cout << " Enter the numbers: \n";
for (i = 0; i< max_num; i++) // input array from user
{
cout << " Number " << i+1 << " is ";
cin >> arr[i];
}
cout <<" Numbers are : ";
for (i = 0; i < max_num; i++)
{
cout << arr[i] << "\t";
}
// use delete operator to deallocate dynamic memory
delete [ ] arr;
return 0;
}
Output
Enter total number of elements to be entered : 7
Enter the numbers:
Number 1 is 45
Number 2 is 600
Number 3 is 78
Number 4 is 93
Number 5 is 29
Number 6 is 128
Number 7 is 32
Numbers are : 45 600 78 93 29 128 32
Program to delete NULL pointer using delete operator
Let's consider a program to delete NULL pointer using the delete operator in C++ programming language.
Program3.cpp
#include <iostream>
using namespace std;
int main ()
{
// initialize the integer pointer as NULL
int *ptr = NULL;
// delete the ptr variable
delete ptr;
cout << " The NULL pointer is deleted.";
return 0;
}
Output
The NULL pointer is deleted.
Delete a pointer with or without value using the delete operator
Let's consider an example to delete a pointer variable with or without a value using the delete operator in C++.
Program4.cpp
#include <iostream>
using namespace std;
int main ()
{
// Use new operator to create dynamic memory
int *ptr = new int;
// Use new operator to dynamic memory space for an array
int *ptr2 = new int (10);
cout << " The value of ptr is: "<< *ptr << " \n ";
cout << " The value of ptr2 is: "<< *ptr2 << " \n ";
// use delete keyword to delete the value stored in *ptr and *ptr2
delete ptr;
delete ptr2;
return 0;
}
Output
The value of ptr is: 1415071048
The value of ptr2 is: 10
Program to allocate dynamic memory using the malloc function and then delete using the delete operator
Let's consider an example of creating dynamic memory using the malloc function and then using the delete operator to delete allocated memory in the C++ programming language.
Program6.cpp
#include <iostream>
using namespace std;
int main ()
{
// create a dynamic memory using malloc() function
char *str = (char *) malloc (sizeof (char));
cout << " Dynamic memory is deleted using the delete operator. " << endl;
delete str; // use delete operator to delete the referencing pointer
return 0;
}
Output
Dynamic memory is deleted using the delete operator.
Program to delete variables of user defined data types
Let's write a program to demonstrate the deletion of user defined object using the delete operator.
Program7.cpp
#include <bits/stdc++.h>
using namespace std;
struct Ptr
{
static void operator delete (void *ptr1, std::size_t sz)
{
cout << " Custom deletion of size " << sz <<endl;
delete (ptr1); // use delete() function
}
static void operator delete[] (void *ptr1, std:: size_t sz)
{
cout << " Custom deletion of size " << sz;
delete (ptr1); // use ::operator delete() function
}
};
int main()
{
Ptr *data = new Ptr; // create dynamic memory
delete data; // delete specific variable
Ptr *data2 = new Ptr[20];
delete[] data2; // delete block of memory
}
Output
Custom deletion of size 1
Custom deletion of size 24
Program to delete a void pointer using the delete operator
Let's create a program to release the memory space of the void pointer using the delete operator in C++.
Program8.cpp
#include <iostream>
using namespace std;
int main ()
{
// declare a void pointer
void *str;
cout << " The void pointer is deleted using the delete operator. " <<endl;
// delete the void pointer reference
return 0;
}
Output
The void pointer is deleted using the delete operator.