CS core subjects

Stage 1 · Lesson 6 of 7

Program memory: the stack and the heap

How a computer runs a program

A running program's memory is split into regions: the code itself, global data, the and the heap. The stack holds one frame for each function call. A frame keeps that call's local variables and the address to go back to when it ends.

The stack grows when a function is called and shrinks when it returns, all automatically. The heap holds data whose size or lifetime is only known while the program runs, such as a list that grows as users sign up. In C you manage heap memory yourself.

In Java, Python or JavaScript a frees it for you. This explains three common bugs. Very deep recursion runs out of stack space, a stack overflow.

Memory you never release piles up, a . And returning a pointer to a local variable is a bug, because that frame is gone once the function returns. The stage builds on this when it maps these regions into .

Common trapthinking garbage collection means memory problems cannot happen.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

Keep handy

  • A topic map of the whole CS curriculum to check your coverage against, stage by stage

  • Short explanations from researchers when one idea, such as floating point, will not click

Also worth your time

Mark this stage as not done?