Step 520–25 minutes
Program Memory Layout
When a program runs, the Operating System gives it a block of memory. The program doesn't just use this block randomly; it organizes it into specific segments.
High Address
Stack
↓
Heap
↑
Data
Code (Text)
Low Address
Stack
LIFO (Last-In, First-Out)Stores local variables and function call information. It grows and shrinks automatically.
Heap
Flexible sizeUsed for dynamic memory allocation. You (or your runtime) manage this space.
Data
Fixed sizeStores global and static variables that exist for the entire life of the program.
Code (Text)
Read-OnlyThe actual machine instructions of your program. Usually read-only.
Stack vs. Heap
The Stack is managed by the CPU and is extremely fast. Every time you call a function, a new "frame" is added to the stack. When the function returns, it's removed. The Heap is for larger, more complex data that needs to stay around longer.