student@ubuntu:~$
c 4/5 40 XP

Memory Layout

0%

Quick Reference

Memory layout (low to high addresses):

+-------------------+  Low addresses
| Text (code)       |  Machine instructions (read-only)
+-------------------+
| Data              |  Initialized globals/statics
+-------------------+
| BSS               |  Uninitialized globals (zeroed)
+-------------------+
| Heap              |  calloc/malloc (grows UP)
|        |          |
|        v          |
|                   |
|        ^          |
|        |          |
| Stack             |  Local variables (grows DOWN)
+-------------------+  High addresses

Lifetime comparison:

Storage Lifetime Example
Stack Until function returns int x = 5;
Heap Until you free() it int *p = calloc(1, sizeof(int));
Data/BSS Entire program static int count = 0;
Text Entire program Your compiled code

Common Pitfalls

  • Returning local addresses – Stack variables die with the function. Never return &local_var.
  • Stack overflow – Deep recursion or large local arrays can exhaust the stack (typically 8 MB).
  • Memory leaks – Heap memory must be explicitly freed. The OS reclaims it at exit, but long-running programs suffer.
  • Writing to text/rodata – String literals and code are read-only. Attempting to modify them crashes the program.

Unlocks

Complete this skill to see what it unlocks.