Dynamic Memory Allocation allows a program to request memory from the operating system while it is running, rather than deciding exactly how much memory is needed at compile time. This is one of the most powerful features of C++, enabling the creation of flexible data structures like linked lists, trees, and resizable arrays.
To understand dynamic memory, you must understand the two primary areas of memory used by your program:
Behavior: Managed automatically by the compiler. When a function is called, its variables are "pushed" onto the stack; when it returns, they are "popped" off.
Limitation: The size must be known at compile time. You cannot declare int arr[n]; if n is a variable chosen by the user (in standard C++).
Risk: The stack is relatively small. Large arrays can cause a Stack Overflow.
Behavior: A large pool of memory available to the program. The programmer manually requests space and must manually release it.
Benefit: Memory allocated here stays alive until you explicitly delete it, even if the function that created it ends.
Requirement: You access heap memory exclusively through Pointers.
In C++, dynamic memory is managed using two specific keywords:
The new operator requests memory from the heap. If successful, it returns the address of that memory.
int* ptr = new int; // Allocates space for one integer.
int* arr = new int[10]; // Allocates space for an array of 10 integers.
Because the system does not automatically reclaim heap memory, you must return it yourself.
delete ptr; // Deallocates a single object.
delete[] arr; // Deallocates an array (the [] is vital for arrays).
Theoretically, dynamic memory introduces a new responsibility: Ownership.
If you allocate memory using new and then change your pointer to point somewhere else without calling delete, the original memory becomes "orphaned." It is still marked as "in use" by the OS, but your program no longer has the address to find it. If this happens in a loop, your program will slowly consume all available RAM and eventually crash.
If you call delete ptr;, the memory is freed, but the variable ptr still holds the old address. If you try to access *ptr again, you are accessing "dead" memory. This is called a Dangling Pointer.
Best Practice: After deleting, set the pointer to nullptr.
In rare cases, if the system runs out of RAM, new will fail. Theoretically, C++ handles this by throwing an std::bad_alloc exception.
This program asks the user how many items they want to store and creates an array exactly that size.
#include <iostream> using namespace std; int main() { int size; cout << "Enter the number of elements needed: "; cin >> size; // 1. ALLOCATION // Creating an array on the HEAP int* dynamicArr = new int[size]; cout << "Enter " << size << " numbers:" << endl; for (int i = 0; i < size; i++) { cin >> dynamicArr[i]; // Accessing like a normal array } cout << "The numbers are: "; for (int i = 0; i < size; i++) { cout << dynamicArr[i] << " "; } cout << endl; // 2. DEALLOCATION // Crucial: We must free the memory! delete[] dynamicArr; // 3. CLEANUP dynamicArr = nullptr; // Avoid dangling pointer return 0; }
| Feature | Static (Stack) | Dynamic (Heap) |
| Keyword | None (Automatic) | new and delete |
| Size | Must be a constant. | Can be a variable. |
| Lifetime | Limited to the function/scope. | Until delete is called. |
| Access | Direct variable name. | Indirectly via pointers. |
| Performance | Very fast. | Slightly slower (OS overhead). |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION