💾 Dynamic Memory in C++ — new, delete and Memory Leaks
Master dynamic memory in C++ — new, delete, new[]/delete[], heap vs stack, memory leaks, and why modern C++ prefers smart pointers. Examples + interview Q&A.
C++ has two places to put data: the stack (fast, automatic, limited size) and the heap (slow, manual, huge).
🏗️ Stack vs heap
- Stack: regular variables (
int x = 5;). Auto-deleted when scope ends. - Heap: dynamically allocated (
new int). YOU are responsible for cleanup.
🆕 new / delete
int* p = new int(42); // allocate on heap
std::cout << *p;
delete p; // free it — required!🆕 new[] / delete[]
int* arr = new int[100]; // array of 100
delete[] arr; // note the []💎 The modern fix: smart pointers
Since C++11, prefer std::unique_ptr and std::shared_ptr from <memory>. They auto-delete when going out of scope (RAII).
#include <memory>
auto p = std::make_unique<int>(42);
// no delete needed — freed automatically💻 Code Examples
Allocate, use, free
int* p = new int(10);
*p += 5;
std::cout << *p; // 15
delete p; // requiredOutput:
15
Dynamic array
int n = 5;
int* a = new int[n];
for (int i = 0; i < n; i++) a[i] = i*i;
for (int i = 0; i < n; i++) std::cout << a[i] << ' ';
delete[] a;Output:
0 1 4 9 16
Modern style — unique_ptr
#include <memory>
auto p = std::make_unique<int>(42);
std::cout << *p;
// no manual delete — frees automatically when p goes out of scopeOutput:
42
⚠️ Common Mistakes
- Forgetting to call delete — memory leak.
- Using `delete` on an array allocated with `new[]` — undefined behavior, must use `delete[]`.
- Double-delete: calling delete twice on the same pointer — crash.
- Using a pointer after delete — dangling pointer.
- Not setting pointer to nullptr after delete — accidental reuse.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What's the difference between stack and heap memory?Beginner
Stack: small, fast, auto-managed by scope. Heap: large, slower, you manage manually via new/delete. Stack stores local variables; heap holds dynamically-sized or long-lived data.
Q2.What is a memory leak?Beginner
Memory allocated on the heap that is never freed. The OS reclaims it when the program exits, but in long-running programs leaks accumulate until the process is killed by the OS.
Q3.What's the difference between delete and delete[]?Intermediate
`delete` frees a single object allocated with `new`. `delete[]` frees an array allocated with `new[]`. Mixing them is undefined behavior.
Q4.What is RAII?Intermediate
Resource Acquisition Is Initialization — bind a resource's lifetime to an object's lifetime. The constructor acquires, the destructor releases. The cornerstone of safe C++ and the principle behind smart pointers and STL containers.
Q5.When would you use shared_ptr vs unique_ptr?Advanced
unique_ptr: SOLE ownership — only one pointer can refer to the resource. Cheap, no overhead. shared_ptr: SHARED ownership — many pointers, reference-counted, deletes when last one is gone. Default to unique_ptr.
🧠 Quick Summary
- Stack: auto, fast, limited. Heap: manual, big.
- new allocates; delete frees. Always match them.
- Arrays: new[] / delete[] — don't mix.
- Leak = forgotten delete. Long programs crash.
- Prefer smart pointers (unique_ptr / shared_ptr) in modern C++.