delete operator in C++


Reading time: 20 minutes

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new operator as well as NULL pointers.

Syntax :

delete <variable name> 
or 
delete [ ] <array_name>

New operator is used for dynamic memory allocation which puts variables on heap memory while delete operator deallocates memory from heap.
Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed/deallocated.

For deleteing the pointer you need to use derefrencing the pointer to prevent dandling or wild pointers which are not good for you programmer.

NOTE : delete operator is only used to delete pointers which a NULL or pointing to something created by using new opertaor.

Need of delete operator

We all know after the program terminates or shuts down the variable are automatically deallocated/deleted but it's important to use delete opertor in order to use the memory allocated in program again if needed and prevent sitution like memory not left or allocation of a space containing some unknown value.

Difference between delete and free()

Both are used for same purpose, but still they have some differences, the differences are :

  1. delete is an operator where as free() is a library function.
  2. delete free the allocated memory and calls destructor while free() de-allocate memory but does not call destructor.
  3. delete is faster than free().

Examples

Deleting Array Objects

In the following C++ code, we are demonstrating how to delete arrays.

#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Allocate Heap memory 
    int* array = new int[10];  
      
    // Deallocate Heap memory 
    delete[] array;  
  
    return 0; 
} 

Deleting NULL pointer

In the following C++ code, we are demonstrating how to delete NULL pointer.

#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // ptr is NULL pointer 
    int* ptr = NULL; 
  
    // deleting ptr 
    delete ptr; 
  
    return 0; 
} 

Deleting pointer with or without value

In the following C++ code, we are demonstrating how to delete pointers.

#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

Deleting variables of User Defined data types

In the following C++ code, we are demonstrating how to delete user defined objects.

#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

In the above example we are using the concept of operator overloading which is availabe in C++ but not in C. In the above program we have overloaded 2 operators delete and delete[] as soon as we run the program and the compiler delete var1; it calls static void operator delete(void* ptr, std::size_t sz) and execute the code mentioned in the function which helps in deleting P. Similarly when the compiler reads delete[] var2; it tend to call static void operator delete[](void* ptr, std::size_t sz) and execute the code mentioned in the function which leads in deleting P[10].

Read about function overloading in C++ which is used to overload delete operator to delete custom user defined objects in C++

Question

Consider the following code:

#include<iostream> 
using namespace std; 
  
int main() 
{ 
    int *ptr = new int; 
    delete ptr; 
    delete ptr; 
    return 0; 
}

Is it fine to call delete twice for a pointer?

No
Yes
Will give error
Depends on operating system
It is undefined behavior to call delete twice on a pointer.Anything can happen, the program may crash or produce nothing as you will be deleting someting not exsisting which won't make sense.
Read about new operator in C++ which you can use to create objects which in turn, can be deleted using delete operator in C++