delete() in C++
Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression.
- Delete can be used by either using Delete operator or Delete [ ] operator
- New operator is used for dynamic memory allocation which puts variables on heap memory.
- Which means Delete operator deallocates memory from heap.
- Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.
Here, Below are examples where we can apply delete operator:
1. Deleting Array Objects: We delete an array using [] brackets.
// Program to illustrate deletion of array #include <bits/stdc++.h> using namespace std; int main() { // Allocate Heap memory int * array = new int [10]; // Deallocate Heap memory delete [] array; return 0; } |
2. Deleting NULL pointer : Deleting a NULL does not cause any change and no error.
// C++ program to deleting // NULLL pointer #include <bits/stdc++.h> using namespace std; int main() { // ptr is NULL pointer int * ptr = NULL; // deleting ptr delete ptr; return 0; } |
3. Deleting pointer with or without value
#include <bits/stdc++.h> using namespace std; int main() { // Creating int pointer int * ptr1 = new int ; // Initializing pointer with value 20 int * ptr2 = new int (20); cout << "Value of ptr1 = " << *ptr1 << "\n" ; cout << "Value of ptr2 = " << *ptr2 << "\n" ; delete ptr1; // Destroying ptr1 delete ptr2; // Detroying ptr2 return 0; } |
Output:
Value of ptr1 = 0 Value of ptr2 = 20
4. Deleting a void pointer
#include <bits/stdc++.h> using namespace std; int main() { void * ptr; // Creating void pointer delete ptr; // Destroying void pointer cout << "ptr deleted successfully" ; return 0; } |
Output:
ptr deleted successfully
5. deleting memory dynamically allocated by malloc
#include <bits/stdc++.h> using namespace std; int main() { // Dynamic memory allocated by using malloc int * ptr2 = ( int *) malloc ( sizeof ( int )); delete ptr2; cout << "ptr2 deleted successfully" ; return 0; } |
Output:
ptr2 deleted successfully
Although above program runs fine on GCC. It is not recommended to use delete with malloc().
6. Deleting variables of User Defined data types:
#include <bits/stdc++.h> using namespace std; struct P { static void operator delete ( void * ptr, std:: size_t sz) { cout << "custom delete for size " << sz <<endl; delete (ptr); // ::operator delete(ptr) can also be used } static void operator delete []( void * ptr, std:: size_t sz) { cout << "custom delete for size " << sz <<endl; delete (ptr); // ::operator delete(ptr) can also be used } }; int main() { P* var1 = new P; delete var1; P* var2 = new P[10]; delete [] var2; } |
Output:
custom delete for size 1 custom delete for size 18
Exceptions:
1. Trying to delete Non-pointer object
#include <bits/stdc++.h> using namespace std; int main() { int x; // Delete operator always // requires pointer as input delete x; return 0; } |
Output:
error: type ‘int’ argument given to ‘delete’, expected pointer
2. Trying to delete pointer to a local stack allocated variable.
#include <bits/stdc++.h> using namespace std; int main() { int x; int * ptr1 = &x; // x is present on stack frame as // local variable, only dynamically // allocated variables can be destroyed // using delete operator delete ptr1; return 0; } |
Output:
Runtime error
Recommended Posts:
- "delete this" in C++
- How to delete last element from a map in C++
- delete and free() in C++
- How to delete last element from a set in C++
- How to delete last element from a List in C++ STL
- Overloading New and Delete operator in c++
- How to delete an element from the Set by passing its value in C++
- Delete elements in C++ STL list
- Program to delete Nth digit of a Number
- new and delete operators in C++ for dynamic memory
- How to delete a range of values from the Set using Iterator
- How to delete a range of values from the List using Iterator
- Different ways to delete elements in std::map (erase() and clear())
- Delete all Non-Prime Nodes from a Singly Linked List
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.