Delete Operator in C++
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:
Similarly, we can delete the block of allocated memory space using the delete  operator.
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.
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++.
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.
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++.
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.
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.
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++.
The void pointer is deleted using the delete operator.