A delete operator is employed to free up memory that has been dynamically assigned using operators such as new, calloc, and malloc during program execution in the C++ language. Essentially, the delete operator is utilized to release both array and non-array (pointer) objects from the heap, where the new operator dynamically assigns memory to store variables. It is possible to use either the delete operator or the delete operator in our codebase to release the deallocated memory. The delete operator has a void return type, meaning it does not return any value.
Syntax of delete operator
We have the ability to remove a particular element or variable by utilizing the delete operator, demonstrated as follows:
delete pointer_variable;
// delete ptr; It deallocates memory for one element
Likewise, we have the option to free up the allocated memory block by utilizing 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 explore an instance where we deallocate memory space for individual variables stored in the heap memory by employing 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 develop a program to deallocate the dynamically allocated memory space for an array object by utilizing 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 explore a program that removes a NULL pointer using the delete keyword in the 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 explore an illustration on deallocating a pointer variable with or without a value utilizing 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 explore an instance of allocating dynamic memory with the malloc function and subsequently deallocating the allocated memory using the delete operator 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 create a program to showcase the removal of a user-defined object by utilizing 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 develop a program to free up the memory allocated to the void pointer by employing 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.