A lesson from the Computer Memory module of the bootcamp. About a 14 minute read.
Every running program splits its memory into regions, and the two that matter are the stack, where function calls keep their local variables, and the heap, where data lives for as long as the program chooses. One is fast, small and cleans up after itself. The other is flexible, slower, and has to be cleaned up by somebody, and nearly every memory bug in existence comes from that somebody getting it wrong.
The STACK holds one frame per active function call: its arguments, its local variables, and the address to return to. A frame is created by moving one pointer when the function is called and destroyed by moving it back when the function returns, so allocation is nearly free and cleanup is automatic, and the stack is small, a few megabytes. The HEAP holds everything else: data that must outlive the function that made it, or whose size is only known while the program runs. It can be as large as your memory, it costs an allocator a search to hand out a block, and every block stays allocated until something frees it. That is the whole difference, and every question about stack versus heap is one of those rules applied: lifetime tied to a call, or lifetime chosen by the program.
The rest of this lesson continues with 6 further sections. See the full curriculum.
Browse all 546 practice puzzles