Updated April 14, 2023
Definition of C++ Delete Operator
Delete is an operator in C++ that can be used to free up the memory blocks that has been allocated using the new operator. It can be used using a Delete operator or Delete[] operator. It destroys the memory block or the value pointed by the pointer. It has no effect on the pointer pointing to the starting address of that memory location. It can also be used for a NULL pointer. The memory being deallocated is from the heap as memory being allocated dynamically using a new operator is on the heap whereas the pointer pointing to the starting address of memory block remains unaltered.
Syntax of delete in C++
For delete operator, we have 2 types of syntax:
- To delete a specific variable operator in C++ language allocated using the new operator.
void operator delete[] (void* ptr1) noexcept;
- To delete a block of memory or array of memory blocks allocated using the new operator.
void operator delete[] (void* ptr1,const std::nothrow_t¬hrow_constant) noexcept;
void operator delete[] (void* ptr1 ,void* ptr2) noexcept;
void operator delete[] (void*ptr1, std::size_tnum) noexcept;
void operator delete[] (void*ptr1, std::size_tnum , conststd::nothrow_t¬hrow_constant) noexcept;
- Here ptr1: refers to the pointer that is pointing to the starting address of the dynamically allocated memory block using the new[] operator. In case a null-pointer is passed to the function nothing is executed. In case the operator has already been released then nothing will be executed.
- Nothrow_constant: This refers to the constant of type nothrow. This parameter is often ignored in the default definition.
- Void ptr2: This refers to the secondary void pointer that is ignored in the case of default definition.
- Num: refers to the number or size of the block of memory to be deallocated. It is a constant of type std::size_t which is an unsigned integer.
How Delete Operator Work in C++?
Delete operator is the same as a regular function in C++ that have some special behavior but can easily be called like another function. When a pointer pointing to the memory block is passed to delete the operator it calls specific destructors for each element in the block of memory followed by an array deallocation method.
In case deallocation needs to be performed for an array of elements then function named as operator delete[] from a class object is called otherwise a global function is called. Also, we must note if we mention any scope operator(:: ) before the delete expression only global functions will be triggered for deallocation.
All the allocation and deallocation functions in C++ have various special components in the libraries having below unique properties:
- Global: The three versions of delete[] operator have been declared in the global namespace, instead of the std namespace.
- Implicit: The declarations of all deallocating versions have been included in all translations unit of a C++ program, irrespective if <new> has been included in the header or not.
- Replaceable: All the reallocation versions present in C++ can easily be overloaded to change its behavior according to our requirements.
In this way allocation and deallocation process in C++ is very interesting and flexible and being enhanced with newer versions. This helps a lot to develop an application with better performance and memory management. These allocations and deallocation functions must be used correctly considering the sequence so that no exceptions or errors are raised.
Example of delete in C++
Some of the examples are given below:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *my_ptr = NULL;
my_ptr = new int();
int *my_var = new int(20);
if(!my_ptr)
{
cout<<"Memory has not been allocated"<<endl;
}
else
{
cout<<"Memory has been allocated successfully"<<endl;
*my_ptr = 20;
cout<<"*my_ptr = "<<*my_ptr<<endl;
cout<<"*my_var = "<<*my_var<<endl;
}
double *myarr = NULL;
myarr = new double[20];
if(!myarr)
{cout<<"Memory has not been allocated to array"<<endl;}
else
{
for(inti=0;i<20;i++)
myarr[i] = i+1;
cout<<"Values in array are : ";
for(inti=0;i<20;i++)
cout<<myarr[i]<<" ";
cout<<endl;
}
delete my_ptr;
delete my_var;
delete[] myarr;
return 0;
}
Output:
Advantages
Delete operator is one of the essential feature in the C++ library that is used most often to deallocate the memory dynamically that has been allocated using the new operator.
- Dynamic Memory Deallocation: Usually when we create arrays at compile time in C++, the size of such arrays exist until the program lasts. In the case at the run time know that we don’t require an array memory after a certain point then that memory space can be easily freed and used by another variable. Thus delete operator helps to free that space.
- Heap Memory: Heap memory is essential at runtime memory allocation thus memory must be managed properly to free up the heap space that is not required and also heap memory also helps a lot in enhancing the performance of the application.
- Enhance Memory Management: The delete operator helps to use dynamic arrays allocated using the new operator and thus is deallocated once they are used so that rest of the space can be utilized.
- Better Lifetime: delete operator helps a lot to maintain the lifetime of the variables and avoiding the segmentation fault error that occurs when the memory space is all used up. Thus improves the performance and lifetime of the variables as well as application.
Conclusion
In C++ delete operator is used to deallocating memory held by a specific pointer that has been allocated dynamically using the new operator. delete operator flushes out the values in those memory blocks on the heap but the pointer pointing to the starting address of the memory block remains unaffected.
Recommended Articles
This is a guide to delete in C++. Here we also discuss the definition and how the delete operator works in c++? along with an example and advantages. You may also have a look at the following articles to learn more –