Dynamic Memory Allocation in C++ – Complete Tutorial
Last Updated on: 18th Dec 2025 16:42:46 PM
In C++, Dynamic Memory Allocation (DMA) is a technique where memory is allocated at runtime instead of compile time. This allows programs to request memory as needed, use it efficiently, and release it when no longer required.
memory can be allocated in two ways:
-
Static (Compile-time) memory allocation
-
Dynamic (Run-time) memory allocation
Dynamic Memory Allocation allows a program to request memory while the program is running, instead of deciding everything in advance.
This makes programs flexible, efficient, and scalable, especially when the amount of data is not known beforehand.
Definition of Dynamic Memory Allocation
Dynamic Memory Allocation is a technique in which memory is allocated and deallocated during program execution using special operators like new and delete.
Real-life example (Basic Idea)
Booking hotel rooms:
-
You book rooms only when guests arrive
-
You cancel (free) rooms when guests leave
Memory is used only when needed.
1. Why Dynamic Memory Allocation is Needed
Problems with Static Memory Allocation
-
Size must be known at compile time
-
Memory wastage if allocated more than required
-
Cannot resize memory during program execution
Benefits of Dynamic Memory Allocation
-
Memory allocated only when required
-
Efficient memory usage
-
Flexible data structures (arrays, linked lists, trees)
-
Useful for real-time and large applications
Real-life example
Buying food daily instead of storing food for the entire year.
2. Dynamic Memory Allocation using new Operator
The new operator:
-
Allocates memory at runtime
-
Returns the address of allocated memory
-
Stores the address in a pointer
Syntax
data_type *pointer = new data_type;
Real-life example
Renting a temporary storage locker only when needed.
Example
#include <iostream>
using namespace std;
int main() {
int *ptr = new int;
*ptr = 40;
cout << "Value stored dynamically: " << *ptr << endl;
return 0;
}
Output
Value stored dynamically: 40
Short Explanation of Output
Memory is allocated at runtime, and the value is stored and accessed using a pointer.
3. Releasing Memory using delete Operator
Memory allocated using new must be released manually using delete . If not released, it causes a memory leak.
Real-life example
Vacating a rented house after use so others can use it.
Complete Program
#include <iostream>
using namespace std;
int main() {
int *ptr = new int;
*ptr = 90;
cout << "Value before deleting memory: " << *ptr << endl;
delete ptr;
ptr = NULL;
return 0;
}
Output
Value before deleting memory: 90
Short Explanation of Output
The memory is released safely, preventing memory leakage.
4. Dynamic Allocation of Array
Dynamic memory allows creating arrays whose size is decided at runtime.
Real-life example
Creating a custom seating arrangement based on the number of guests.
Complete Program
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of students: ";
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = (i + 1) * 10;
}
cout << "Marks: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
delete[] arr;
return 0;
}
Output
Enter number of students: 3
Marks: 10 20 30
Short Explanation of Output
An array is created dynamically based on user input and deleted after use.
5. Dynamic Memory Allocation for Objects
Objects of a class can also be created dynamically using new.
Real-life example
Hiring a temporary employee for a project.
Example (Incorrect Program)
int *ptr = new int;
// memory not deleted
Correct Practice
delete ptr;
7. Difference Between Static and Dynamic Memory Allocation
| Feature | Static | Dynamic |
|---|---|---|
| Allocation time | Compile time | Run time |
| Size | Fixed | Flexible |
| Memory usage | Less efficient | Efficient |
| Control | Limited | Full control |
Advantages of Dynamic Memory Allocation
-
Efficient memory usage
-
Flexible data size
-
Essential for data structures
-
Better program performance
Disadvantages of Dynamic Memory Allocation
-
Manual memory management
-
Risk of memory leaks
-
Complex debugging
Summary
-
Dynamic memory is allocated at runtime
-
newallocates memory -
deletefrees memory -
Arrays and objects can be created dynamically
-
Proper memory management is essential
Keep practicing — you're doing amazing!
Happy Coding! ![]()