Skip to content
Related Articles

Related Articles

delete keyword in C++
  • Difficulty Level : Easy
  • Last Updated : 04 Feb, 2021

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.
  • The delete operator has void return type does not return a value.

Here, Below are examples where we can apply delete operator:
1. Deleting Array Objects: We delete an array using [] brackets.
 

CPP




// 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.
 

CPP




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

CPP




#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
 

CPP




#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 
 

CPP




#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:
 

CPP




#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
 

CPP




#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. 
 

CPP




#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

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :