Heap Allocation with new/delete
Manage dynamic memory.
Heap Allocation with new/delete is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 automaticallyThe 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.
Frequently asked questions
Is the “Heap Allocation with new/delete” lesson free?
Yes — the full text of “Heap Allocation with new/delete” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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
- Stack Allocation
- Heap Allocation with new/delete
- Object Lifetime
- Common Memory Bugs