![]() |
坐怀不乱的大葱 · Android Kotlin+Data ...· 1 周前 · |
![]() |
酷酷的金针菇 · 《GPAW学习笔记》之安装GPAW - ...· 1 月前 · |
![]() |
粗眉毛的松鼠 · Support "format" in ...· 4 月前 · |
![]() |
文雅的牛肉面 · 南京市人才安居办法(试行) - ...· 5 月前 · |
![]() |
想发财的脸盆 · 宁波鲲池小学6月20日电脑派位摇号结果公布, ...· 6 月前 · |
![]() |
奔跑的煎饼果子 · 私有化部署开源 AI 大模型推荐· 7 月前 · |
delete is an operator that is used to destroy array and non-array (pointer) objects which are dynamically created by the new operator.
Below are some examples of where we can apply the delete operator:
Understanding the
delete
keyword is crucial for memory management in C++. The
C++ Course
provides insights into dynamic memory allocation and the correct usage of
delete
to avoid memory leaks.
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 gives no error.
// 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++ 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++ 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++ 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++ 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++ 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++ 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
- Company
- About Us
- Legal
- In Media
- Contact Us
- Advertise with us
- GFG Corporate Solution
- Placement Training Program
- GeeksforGeeks Community
- DSA
- Data Structures
- Algorithms
- DSA for Beginners
- Basic DSA Problems
- DSA Roadmap
- Top 100 DSA Interview Problems
- DSA Roadmap by Sandeep Jain
- All Cheat Sheets
- Computer Science
- Operating Systems
- Computer Network
- Database Management System
- Software Engineering
- Digital Logic Design
- Engineering Maths
- Software Development
- Software Testing
- System Design
- High Level Design
- Low Level Design
- UML Diagrams
- Interview Guide
- Design Patterns
- OOAD
- System Design Bootcamp
- Interview Questions
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
![]() |
粗眉毛的松鼠 · Support "format" in ApiModelProperty and correct default Date format · Issue #27 · nestjs/swagger · 4 月前 |
![]() |
奔跑的煎饼果子 · 私有化部署开源 AI 大模型推荐 7 月前 |