delete is an operator that is used to destroy array and non-array(pointer) objects which are dynamically created by the new operator.
- delete can be used by either using the delete operator or delete [ ] operator.
- The new operator is used for dynamic memory allocation which stores variables on heap memory.
- This means the delete operator deallocates memory from the heap.
- The pointer to the object is not destroyed, the value or memory block pointed by the pointer is destroyed.
- The delete operator has void return type which means it does not return any value.
Below are some examples of where we can apply the delete operator:
1. Deleting Array Objects
We delete an array using [] brackets.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * array = new int [10];
delete [] array;
return 0;
}
|
2. Deleting NULL Pointer
Deleting a NULL does not cause any change and gives no error.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * ptr = NULL;
delete ptr;
return 0;
}
|
3. Deleting Pointer With or Without Value
The memory pointed out by the specified pointer will be deallocated from the heap memory.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * ptr1 = new int ;
int * ptr2 = new int (20);
cout << "Value of ptr1 = " << *ptr1 << "\n" ;
cout << "Value of ptr2 = " << *ptr2 << "\n" ;
delete ptr1;
delete ptr2;
return 0;
}
|
OutputValue of ptr1 = 0
Value of ptr2 = 20
4. Deleting a Void Pointer
The delete operator does not only deallocate the memory, but it also calls the destructor of the object to be deleted. That is why, if we use void pointer with delete, it will lead to undefined behaviour.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
void * ptr;
delete ptr;
cout << "ptr deleted successfully" ;
return 0;
}
|
Outputptr deleted successfully
5. Deleting Memory Dynamically Allocated by malloc()
Deallocating memory allocated by malloc() using the delete operator also leads to undefined behavior. It is recommended to use delete for new and free() for malloc.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int * ptr2 = ( int *) malloc ( sizeof ( int ));
delete ptr2;
cout << "ptr2 deleted successfully" ;
return 0;
}
|
Outputptr2 deleted successfully
Note: Although the above program runs fine on GCC. It is not recommended to use delete with malloc().
6. Deleting Variables of User-Defined Data Types
C++
#include <bits/stdc++.h>
using namespace std;
struct P {
static void operator delete ( void * ptr, size_t sz)
{
cout << "custom delete for size " << sz << endl;
::operator delete (ptr);
}
static void operator delete []( void * ptr, size_t sz)
{
cout << "custom delete for size " << sz << endl;
::operator delete (ptr);
}
};
int main()
{
P* var1 = new P;
delete var1;
P* var2 = new P[10];
delete [] var2;
}
|
Outputcustom delete for size 1
custom delete for size 18
Exceptions
1. Trying to Delete a Non-Pointer Object
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x;
delete x;
return 0;
}
|
Output
error: type ‘int’ argument given to ‘delete’, expected pointer
2. Trying to Delete the Pointer to a Local Stack-Allocated Variable
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x;
int * ptr1 = &x;
delete ptr1;
return 0;
}
|
Output
main.cpp: In function ‘int main()’:
main.cpp:16:12: warning: ‘void operator delete(void*, std::size_t)’ called on unallocated object ‘x’ [-Wfree-nonheap-object]
16 | delete ptr1;
| ^~~~
main.cpp:9:9: note: declared here
9 | int x;
| ^
free(): invalid pointer
Related Articles