Stage 1 · Lesson 6 of 7
Program memory: the stack and the heap
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
- CourseFreeCS50: Introduction to Computer Science
Harvard University · Beginner
The first weeks in C teach binary, memory, pointers and the stack and heap with excellent problem sets
- VideoFreeCrash Course Computer Science
CrashCourse · Beginner
Short videos from transistors to CPUs and memory; the quickest way to see the whole picture