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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


2. Deleting NULL pointer : Deleting a NULL does not cause any change and no error.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


3. Deleting pointer with or without value



filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

Value of ptr1 = 0
Value of ptr2 = 20

4. Deleting a void pointer

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

ptr deleted successfully

5. deleting memory dynamically allocated by malloc

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

custom delete for size 1
custom delete for size 18

Exceptions:

1. Trying to delete Non-pointer object

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int x;
  
    // Delete operator always
    // requires pointer as input
    delete x;
  
    return 0;
}

chevron_right


Output:

error: type ‘int’ argument given to ‘delete’, expected pointer

2. Trying to delete pointer to a local stack allocated variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Output:

Runtime error


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.