100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Dynamic Memory Allocation in C++

Learn how `new` and `delete` allocate and free heap memory at runtime, and why forgetting to `delete` causes a memory leak.

PointersIntermediate8 min readJul 7, 2026
Analogies

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

cpp
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

cpp
#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

text
*p = 7
Array: 10 20 30 40

6. Key Takeaways

  • new allocates memory on the heap at runtime and returns a pointer to it; delete frees 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 plain new/delete together for single objects; never mix them.
  • Forgetting to delete allocated memory causes a memory leak, wasting memory for the rest of the program's execution.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#DynamicMemoryAllocationInC#Dynamic#Memory#Allocation#Syntax#StudyNotes#SkillVeris#ExamPrep