C++ Academy · Lesson

Heap Allocation with new/delete

Manage dynamic memory.

Lesson 2 of 413 steps

Heap Allocation with new/delete is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

Dynamic Memory with new

The new operator allocates an object on the heap (free store) and returns a pointer to it. Heap objects live until you explicitly free them.

#include <iostream>

int main() {
    int* p = new int(42);   // heap allocation
    std::cout << *p << "\n";
    delete p;               // release it
}

Releasing with delete

Every new must be matched by exactly one delete. Forgetting it leaks memory; doing it twice corrupts the heap.

#include <iostream>

int main() {
    double* d = new double(3.5);
    std::cout << *d << "\n";
    delete d;     // exactly once
}

Arrays: new[] and delete[]

For arrays use new[] and the matching delete[]. Mixing new[] with plain delete is undefined behavior.

#include <iostream>

int main() {
    int* arr = new int[3]{1, 2, 3};
    for (int i = 0; i < 3; ++i) std::cout << arr[i] << "\n";
    delete[] arr;   // note the [ ]
}

Why Use the Heap?

Use the heap when an object must outlive the function that created it, or when its size is only known at runtime. Otherwise prefer the stack.

Heap Lifetime is Manual

Unlike the stack, the heap does not free objects automatically. You control exactly when memory is released — and you are responsible for it.

#include <iostream>

int* makeCounter() {
    return new int(0);   // survives the function
}

int main() {
    int* c = makeCounter();
    std::cout << *c << "\n";
    delete c;
}

Set Pointers to nullptr After delete

After deleting, set the pointer to nullptr so accidental reuse fails loudly instead of touching freed memory.

#include <iostream>

int main() {
    int* p = new int(9);
    delete p;
    p = nullptr;     // now p is safe to check
    if (!p) std::cout << "freed\n";
}

Allocation Can Fail

If the system is out of memory, new throws std::bad_alloc. For very large allocations this is a real possibility.

Runtime-Sized Buffers

Heap arrays can use a size computed at runtime — something stack arrays cannot do portably.

#include <iostream>

int main() {
    int n = 5;
    int* buf = new int[n];
    for (int i = 0; i < n; ++i) buf[i] = i * i;
    std::cout << buf[4] << "\n";
    delete[] buf;
}

Prefer Smart Pointers

Modern C++ avoids raw new/delete. std::unique_ptr and std::make_unique free memory automatically when the pointer goes out of scope.

#include <iostream>
#include <memory>

int main() {
    auto p = std::make_unique<int>(42);
    std::cout << *p << "\n";
}   // freed automatically

The Cost of the Heap

Heap allocation is slower than the stack: it searches for a free block and updates bookkeeping. Frequent tiny allocations can hurt performance.

Match Every new

The golden rule: every new needs one delete, every new[] needs one delete[]. Better still, let smart pointers handle it for you.

Quick Check

Test your understanding of heap allocation.

Recap

You learned to allocate on the heap with new/new[] and release with delete/delete[]. Heap objects outlive their scope but require manual cleanup. Match every allocation, null out freed pointers, and prefer std::unique_ptr in modern code.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Heap Allocation with new/delete” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Heap Allocation with new/delete”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Heap Allocation with new/delete”?

Manage dynamic memory. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Heap Allocation with new/delete” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stack Allocation
  2. Heap Allocation with new/delete
  3. Object Lifetime
  4. Common Memory Bugs
← Back to C++ Academy