Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Concepts and Tools

Program memory layout in C

Reference for how a C program is laid out in memory: code, read-only data, initialized and zero-initialized globals, the heap and the stack, with checks you can run.

A running C program does not see one flat block of memory. The operating system gives it an address space divided into regions, each with its own rules: code that can be run but not changed, constants that can be read but not changed, global variables, a heap that grows when you call malloc, and a stack that grows with every function call. The C standard never mentions these regions. It talks about storage duration (static, automatic, allocated, thread), and the regions below are how real systems implement those durations.

Knowing the layout explains a lot of everyday C: why a modified string literal crashes, why a returned pointer to a local array is wrong, why a huge local array overflows the stack but a huge global one does not, and why static variables keep their value.

The regions

The picture below is typical for Linux on x86-64. Names and exact order differ between systems, and address randomization moves everything on each run.

TEXT
high addresses
  +-----------------------------+
  | arguments and environment   |  argv[], envp[]
  +-----------------------------+
  | stack                       |  automatic variables, return addresses; grows down
  |            |                |
  |            v                |
  |   (unmapped gap)            |
  |            ^                |
  |            |                |
  | heap                        |  malloc/calloc/realloc; grows up (or uses mmap)
  +-----------------------------+
  | bss                         |  static/global objects with no initializer (zero)
  +-----------------------------+
  | data                        |  static/global objects with a non-zero initializer
  +-----------------------------+
  | rodata                      |  string literals, const globals, jump tables
  +-----------------------------+
  | text                        |  machine code
  +-----------------------------+
low addresses
Region Holds Writable Lifetime
text your functions no (read and execute) whole run
rodata "literals", const globals no whole run
data initialized globals and static objects yes whole run
bss zero-initialized globals and static objects yes whole run; starts all zero
heap blocks from malloc yes until free
stack locals, parameters, saved registers yes until the function returns

Try it

C Which region does each object live in?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C The stack grows with every call

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Usage notes

  • Text and rodata are read-only. Writing to a string literal (char *s = "hi"; s[0] = 'H';) is undefined behavior and typically crashes. Use char s[] = "hi"; to get a writable copy on the stack, or declare const char *s.
  • bss costs nothing in the file. A global int table[1000000]; adds no bytes to the executable: the loader provides zeroed pages. A global with a non-zero initializer must be stored in the file's data segment.
  • Stack size is limited. Typically 8 MB on Linux and 1 MB on Windows. Large local arrays and deep recursion overflow it; the result is a crash, not an error code. Put big buffers in static storage or on the heap.
  • Heap blocks come from two places. Small requests extend the program's heap; large ones (often above 128 KB with glibc) are separate memory mappings. See mmap. free may or may not return memory to the operating system.
  • Address-space randomization (ASLR) shifts every region on each run, which is why the examples print comparisons and never addresses. Do not depend on a particular order in portable code.
  • Comparing unrelated pointers with < is not defined by ISO C. The examples convert to uintptr_t for illustration only.
  • Threads each get their own stack; globals, the heap and static locals are shared. _Thread_local objects have one copy per thread.
  • Tools. size program shows the text, data and bss sizes; nm lists symbols by region; on Linux /proc/self/maps shows the live mappings.