In C++, the `delete` operator is used to deallocate memory that was previously allocated using the `new` operator. When you use `delete`, you are returning that allocated storage to the free store (also known as the heap) so that it can be re-used for subsequent memory allocation requests.
It's crucial to use `delete` for every `new` to prevent memory leaks, where allocated memory is no longer accessible by the program but hasn't been returned to the free store.
In C++, the heap (also known as free store) refers to a region of memory used for dynamic memory allocation. Here are the key characteristics of the heap within the context of the C++ programming language:
🔑 Dynamic Lifetime
Memory allocated on the heap persists until it is explicitly deallocated.
This contrasts with stack variables, which are automatically destroyed when they go out of scope.
✅ Example:
int* ptr = new int(42); // Allocates memory on the heap
// ...
delete ptr; // Must be explicitly deallocated
🧠 Managed via `new` and `delete` Operators
`new` allocates memory and returns a pointer.
`delete` deallocates the memory and must be used carefully to avoid memory leaks.
✅ For Arrays:
int* arr = new int[10]; // Allocates array of 10 ints on heap
delete[] arr; // Must use delete[] for arrays
⚠️ Manual Memory Management
Heap memory management is not automatic.
Programmers are responsible for:
Avoiding memory leaks (forgetting to `delete`)
Avoiding dangling pointers (using memory after `delete`)
Preventing double deletions
⏱️ Slower Allocation Compared to Stack
Heap allocation involves system calls and is generally slower than stack allocation.
It is also more prone to fragmentation.
📦 Used for Dynamic Data Structures
Ideal for creating objects or structures whose size or lifetime is not known at compile time.
Commonly used for:
Linked lists
Trees
Graphs
Dynamic arrays
🧹 Modern Alternatives: Smart Pointers
C++11 introduced smart pointers like `std::unique_ptr` and `std::shared_ptr` to manage heap memory automatically.
#include
std::unique_ptr uptr(new int(42)); // No manual delete needed
🧮 Shared Across Threads
The heap is generally shared among threads, making it useful for multithreaded applications where data needs to outlive function scopes.
🧰 Summary Table:
Feature
Heap Characteristics in C++
Lifetime
Manual, until `delete` is called
Allocation Keyword
`new`, `new[]`
Deallocation Keyword
`delete`, `delete[]`
Speed
Slower than stack
Flexibility
High (size and lifetime are dynamic)
Risk
Leaks, dangling pointers, double delete
Safe Practice
Use smart pointers (C++11 and later)
The following diagram shows the relationship between the heap, constructor and destructor
Examine the use of the operator delete to deallocate free store memory.
The operator delete destroys a variable created by new, in effect returning its allocated storage to free store for re-use. The operator delete returns type void and has two forms.
Non-array form
When the corresponding new expression did not allocate an array, you use this form:
deleteexpression
Example
When
p = new my_type;
is executed, you use
delete p;
to deallocate the free store used by p.
Array form
When the corresponding new expression allocated an array, you use this form: delete[]expression
Example
When
p = new my_type[size];
is executed, you use
delete []p;
to deallocate the free store used by p.
In the next lesson, we will look at how new and delete are used to dynamically allocate an array.