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++
// 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 gives no error.
C++
// C++ program for 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
The memory pointed out by the specified pointer will be deallocated from the heap memory.
C++
// C++ program for 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";
// Destroying ptr1
delete ptr1;
// Destroying ptr2
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++
// C++ prgram for deleting a void pointer
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating void pointer
void* ptr;
// Destroying void pointer
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++
// C++ program for 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;
}
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++
// C++ program for deleting variables of User Defined data
// types
#include <bits/stdc++.h>
using namespace std;
struct P {
// Overloading delete operator for single object
// deallocation
static void operator delete(void* ptr, size_t sz)
{
cout << "custom delete for size " << sz << endl;
// ::operator delete(ptr) can also be used
::operator delete(ptr);
}
// Overloading delete operator for array deallocation
static void operator delete[](void* ptr, size_t sz)
{
cout << "custom delete for size " << sz << endl;
// ::operator delete(ptr) can also be used
::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++
// C++ program for trying to delete a 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 the Pointer to a Local Stack-Allocated Variable
C++
// C++ program for trying to delete the 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
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
Similar Reads
Const keyword in C++
In this article, the various functions of the const keyword which is found in C++ are discussed. Whenever const keyword is attached with any method(), variable, pointer variable, and with the object of a class it prevents that specific object/method()/variable to modify its data items value.Constant
10 min read
Deleting Memory in C
In C programming, we might allocate memory dynamically for various tasks but what happens when those pieces of memory are no longer needed? If not managed properly, they can lead to memory leaks, wasting valuable resources, and slowing down our program. Therefore, we need to manage memory in C by pr
5 min read
"delete this" in C++
Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered.1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefi
1 min read
C++ Keywords
Keywords are the reserved words that have special meanings in the C++ language. They are the words that have special meaning in the language. C++ uses keywords for a specifying the components of the language, such as void, int, public, etc. They can't be used for a variable name, function name or an
2 min read
Using Keyword in C++ STL
The using keyword in C++ is a tool that allows developers to specify the use of a particular namespace. This is especially useful when working with large codebases or libraries where there may be many different namespaces in use. The using keyword can be used to specify the use of a single namespace
6 min read
Deletion in B+ Tree
B + tree is a variation of the B-tree data structure. In a B + tree, data pointers are stored only at the leaf nodes of the tree. In a B+ tree structure of a leaf node differs from the structure of internal nodes. When compared to the B tree, B+ Tree offers more effective insertion, deletion, and ot
15+ min read
Overloading New and Delete operator in c++
The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes. If these operators are overloaded using member function for a class, it means that these operators are overloaded only
5 min read
delete and free() in C++
delete and free() in C++ have similar functionalities but they are different. In C++, the delete operator should only be used for deallocating the memory allocated either using the new operator or for a NULL pointer, and free() should only be used for deallocating the memory allocated either using m
2 min read
Vector clear() in C++ STL
In C++, vector clear() is a built-in method used to remove all elements from a vector, making it empty. In this article, we will learn about the vector clear() method in C++.Letâs take a look at an example that illustrates the vector clear() method:C++#include <bits/stdc++.h> using namespace s
2 min read
Vector erase() in C++ STL
In C++, vector erase() is a built-in function that is used to delete elements from the vector. It removes an element of a specific position or range of elements from the vector. Letâs take a simple example that uses the vector erase() method:C++#include <bits/stdc++.h> using namespace std; int
3 min read