LearnThatStack Ace your next interview
Operating Systems · question
Question 4 of 56

What is the difference between the stack and the heap in memory?

beginner
← All Operating Systems questions
Re-explain
Visual ↓

The stack holds local variables and call frames that last until their function returns, while the heap holds memory you allocate and free yourself.

The stack is automatic and fast. Each function call pushes a frame, and returning pops it. The compiler must know each frame's size up front, and the return frees that space at once.

The heap is flexible. You allocate a block, use it as long as you like, then free it yourself.

That flexibility has a cost. Heap allocation is slower, and the heap can fragment over time. Forget to free a block, and you leak memory.

Use the stack for short-lived, fixed-size data, and the heap when the size is unknown or the data must outlive the call.

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?
Guided practice for Operating Systems One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
A stack frame pops but its heap block stays
before the callint total = 3;
Stack
total3

Main's frame, its slice of the stack, holds the local variable total. The heap is empty so far.

inside load()char *p = malloc(n);
Stack
total3
nnew64
pnew1
Heap
1malloc blocknew
size64 bytes
freedno
pointed to byp

Calling load pushes a new frame with n and p. Malloc reserves a heap block, and p points to it.

after load() returnsreturn; // no free(p)
Stack
total3
Heap
1malloc block
size64 bytes
freedno
pointed to bychangednothing

Returning pops load's frame, so n and p vanish. The block stays and nothing can free it. It leaked.

Returning frees a function's stack frame at once. A heap block stays until you free it, so losing its only 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 How interviewers actually use this question

Likely follow-ups

  • Why is allocating on the stack faster than allocating on the heap? Contrast bumping the stack pointer with the allocator searching for a free block and updating bookkeeping. The top of the stack is usually in cache.
  • What goes wrong if a function returns a pointer to one of its local variables? Say the return pops the frame, so the pointer dangles and a later call can overwrite that memory. Fix it with a heap allocation or a caller-owned buffer.
  • If two threads run the same function, which memory do they share, the stack or the heap? Each thread has its own stack and all threads share the heap, so heap data that two threads touch needs synchronization.
  • Why does the stack have a fixed size limit while the heap can keep growing? Mention that each stack is one contiguous region with a size cap set up front, while the allocator can ask the OS for more memory when the heap runs short.
  • Where does malloc get heap memory from, and does free hand it back to the OS? Name brk and mmap, and say free usually puts blocks back on the allocator's free lists for reuse, while large mmap blocks go straight back to the OS.

What you can say

  1. The stack holds local variables and call frames that live until their function returns, and the heap holds memory you allocate and free yourself.
  2. Every function call pushes a frame onto the stack and returning pops it, so the stack is automatic and fast.
  3. The catch is that the compiler has to know each frame's size up front, and the return frees all of that space at once.
  4. The heap is the flexible one. You allocate a block, keep it as long as you need, and free it yourself when you're done.
  5. That flexibility costs you. Heap allocation is slower, the heap can fragment over time, and if you forget to free a block you leak memory.
  6. So I put short-lived, fixed-size data on the stack, and I use the heap when the size isn't known ahead of time or the data has to outlive the call.

Weak answers to avoid

  • Says the stack is fast and the heap slow, then stops Speed alone does not tell you which to pick. Explain lifetime and size. The stack frees on return and needs a known size, and heap data can outlive the call.
  • Mixes up the memory heap with the heap data structure A heap allocator is not a priority queue, and mixing the two up signals shaky basics. The memory heap is a pool the allocator hands blocks out of.
  • Leaves out who frees heap memory That drops the main cost. Say heap blocks stay until you free them, or a garbage collector does, and that a forgotten free leaks memory.
  • Ties placement to type, like objects on heap, ints on stack Placement follows lifetime and size, not type. In C or C++ a struct can sit on the stack and an int on the heap.
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 Operating Systems cheatsheet.

← Back to all Operating Systems questions
Pro · $10/mo

49 of 56 Operating Systems 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