LearnThatStack Ace your next interview
C++ · question
Question 1 of 65

What is the difference between stack and heap memory?

beginner
← All C++ questions
Re-explain
Visual ↓

The compiler manages stack memory for you, while heap memory is something you request and give back yourself.

The stack holds function locals and parameters. Each call pushes a frame, the block that holds that call's locals, and each return pops it. So cleanup is automatic and free. The stack is small, often one to eight megabytes. Deep recursion or a huge local array can overflow it and crash the process.

The heap is a separate pool you allocate from at runtime. Objects there live until something frees them, so they can outlive the function that created them. Allocation costs a real call into the allocator, and memory you never release leaks.

Default to the stack. Use the heap when an object must outlive the current scope or is too big for a frame. Also use it when the size is known only at runtime.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
A heap block outlives its function
During the callint* p = new int(42);
Stack
p1
Heap
1int
value42

The local p sits in the stack frame of make(). new gets a heap block, and p holds its address.

After the returnint* q = make();
Stack
qnew1
Heap
1int
value42

make() returned the address in p. Its frame is gone, and p with it, but the heap block stays and q now holds its address.

Pointer droppedq = nullptr; // no delete
Stack
qchangednullptr
Heap
1int
value42

The caller drops q without delete. The block stays allocated with no pointer to it, so it leaked.

The compiler pops stack locals when a call returns. A heap block stays until you delete it, so losing its last pointer leaks it.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C++ cheatsheet.

← Back to all C++ questions
Pro · $10/mo

57 of 65 C++ answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime