Programming foundations

Stage 3 · Lesson 7 of 9

Memory: the stack and the heap

How computers run your code

A running program uses two main areas of memory. The holds a frame for each call in progress, with that call's . It grows when you call a function and shrinks when the function returns.

That makes it fast and automatic, but small, so very deep overflows it. The heap holds data whose size or lifetime is not tied to one call, such as lists, objects and long strings. You reach it through references or pointers (stored addresses).

In C++ you control how long heap data lives, ideally through std::vector and . Python, Java, JavaScript and C# use garbage collection instead: the runtime frees unused heap objects for you. Many "why did this change?" bugs come from two references to one heap object.

A question to check yourself, an AI practice session and 1 cheat sheet entry come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

Keep handy

  • Quick explanations of OS basics like processes and memory when a lecture skips a step

Also worth your time

  • Everything from a key press to a rendered page; read it after this stage

Mark this stage as not done?