The Runtime Theory
easyReference#memory-management

Identify C++ Memory Errors

Given code snippets, identify whether the error is a memory leak, dangling pointer, stack overflow, or double-free.

The Runtime Theory Team1 min read
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inint* p = new int(42); return 0;

outMemory leak — allocated memory is never freed with delete.

inint& bad() { int x = 5; return x; }

outDangling reference — x is destroyed when the function returns; the reference points to freed stack memory.

invoid f() { int arr[1000000000]; f(); }

outStack overflow — each recursive call allocates a huge array on the stack, eventually exhausting stack space.

inint* p = new int(42); delete p; delete p;

outDouble-free — deleting the same memory twice causes undefined behavior (possible corruption or crash).

Identify C++ Memory Errors

Memory management is the most error-prone aspect of C++. Here are the four most common bugs.

Memory Leak

cpp
int* p = new int(42);
// Forgot: delete p;  ← memory is never returned to the heap

Dangling Pointer

cpp
int& bad() {
    int x = 5;
    return x;  // x is destroyed when function returns
}
// Caller receives a reference to dead stack memory

Stack Overflow

cpp
void infinite() {
    int arr[1000000];  // 4 MB per call
    infinite();        // never returns
}

Double-Free

cpp
int* p = new int(42);
delete p;
delete p;  // undefined behavior — the memory is already freed

One dispatch a week

The trace behind each problem, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.

Not started

Sign in to save your learning progress.

Sign in to save