1. Intro
Dynamic memory allocation is the process of requesting and releasing memory at runtime, on a region called the heap, rather than relying on the compiler to reserve fixed storage ahead of time on the stack. In C++, this is done with the new and delete operators, which give programs flexibility to allocate exactly as much memory as they need, when they need it — for example, when the size of data isn't known until the program is running.
Cricket analogy: Dynamic memory allocation is like a stadium adding temporary overflow seating on match day once the actual ticket demand (new) is known, rather than fixing seat count months ahead — and those temporary stands must be explicitly dismantled (delete) after the match ends.
2. Syntax
int* p = new int; // allocate a single int on the heap
int* p2 = new int(42); // allocate and initialize to 42
int* arr = new int[10]; // allocate an array of 10 ints
delete p; // free a single object
delete p2;
delete[] arr; // free an array (note the [])3. Explanation
Ordinary ("automatic") variables declared inside a function live on the stack and are created and destroyed automatically as the function is entered and exited — their lifetime is tied to scope. Dynamically allocated memory is different: new requests a block of memory from the heap at runtime and returns a pointer to it; that memory persists until it is explicitly released, regardless of scope. This is essential when you need data to outlive the function that created it, or when the required size is only known at runtime. Every successful new must be paired with exactly one delete (or new[] with delete[]) to free the memory back to the system when it's no longer needed. If you forget to delete memory you allocated, that memory stays reserved for the lifetime of the program even though nothing can reach it anymore — this is called a memory leak, and repeated leaks can exhaust available memory in long-running programs. Using delete[] on memory allocated with plain new, or vice versa, or deleting the same pointer twice, are also errors to avoid.
Cricket analogy: A stack variable is like a substitute fielder who's automatically off the field once the specific over ends (scope), while heap memory from new is like a permanent squad signing that stays contracted until explicitly released (delete) — useful when a player needs to outlast a single match. Forgetting to release the contract causes a 'memory leak' where the team keeps paying a player nobody uses; releasing the same contract twice is also a costly error.
Every new must be matched with exactly one delete (and new[] with delete[]). Forgetting to delete heap memory causes a memory leak: the memory stays reserved and unreachable for the rest of the program's run.
4. Example
#include <iostream>
using namespace std;
int main() {
int* p = new int(7); // single dynamic int
cout << "*p = " << *p << endl;
delete p; // free it
int n = 4;
int* arr = new int[n]; // dynamic array, size decided at runtime
for (int i = 0; i < n; i++) {
arr[i] = (i + 1) * 10;
}
cout << "Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
delete[] arr; // free the array
return 0;
}5. Output
*p = 7
Array: 10 20 30 406. Key Takeaways
newallocates memory on the heap at runtime and returns a pointer to it;deletefrees it.- Heap memory persists until explicitly freed — unlike stack variables, which are destroyed automatically when they go out of scope.
- Use
new[]/delete[]together for arrays, and plainnew/deletetogether for single objects; never mix them. - Forgetting to
deleteallocated memory causes a memory leak, wasting memory for the rest of the program's execution.
Practice what you learned
1. What does the `new` operator do in C++?
2. How does dynamically allocated (heap) memory differ from an automatic (stack) variable's storage?
3. Which pair correctly matches allocation and deallocation for a dynamic array?
4. What is a memory leak?
5. Why might a program need dynamic memory allocation instead of ordinary stack variables?
Was this page helpful?
You May Also Like
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Dangling Pointers in C++
Understand how a pointer becomes 'dangling' after its target is freed or goes out of scope, and the safe practices that prevent it.
Pointer to Pointer in C++
Learn how a pointer can itself store the address of another pointer, requiring double dereferencing to reach the underlying value.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics