smhk

C memory layout

The memory used by a C program is laid out in various segments:

C memory layout
+-------------------------+    <== High address            
|                         |
| Command line arguments  |
| & environment variables |
|                         |
+-------------------------+
|                         |  \
|          Stack          |   }
|          ~~~~~          |   }
|                         |   }
+ . . . . . . . . . . . . +   }
|           |||           |   }
|           v|v           |   }
|            v            |   }
|                         |   \    Dynamic
|       Free memory       |    }-- memory
|                         |   /    layout
|            ^            |   }
|           ^|^           |   }
|           |||           |   }
+ . . . . . . . . . . . . +   }
|                         |   }
|          Heap           |   }
|          ~~~~           |   }
|                         |  /
+-------------------------+
|                         |  \
|          BSS            |   }
|   (Uninitialized data)  |   }
|                         |   }
+-------------------------+   }
|                         |   \    Static
|          Data           |    }-- memory
|    (Initialized data)   |   /    layout
|                         |   }
+-------------------------+   }
|                         |   }
|          Text           |   }
|         (Code)          |   }
|                         |  /
+-------------------------+    <== Low address
+-------------------------+
|                         |
| Command line arguments  |
| & environment variables |
|                         |



































 





+-------------------------+
|                         |
|          Stack          |
|          ~~~~~          |
|                         |
+ . . . . . . . . . . . . +





























 




















+ . . . . . . . . . . . . +
|                         |
|          Heap           |
|          ~~~~           |
|                         |
+-------------------------+














 


























|                         |
|          BSS            |
|   (Uninitialized data)  |
|                         |
+-------------------------+









 































|                         |
|          Data           |
|    (Initialized data)   |
|                         |
+-------------------------+




 




































|                         |
|          Text           |
|         (Code)          |
|                         |
+-------------------------+

Segment definitions §

The segments are defined as follows:

Command line arguments and environment variables
Contains the arguments passed into the program when it is executed, e.g. argc & argv. They are specified after the name of the program. Also contains the environment variables as key-value pairs (potentially accessible via the non-standard, null-terminated envp).
Stack
Contains the call stack, a LIFO structure. Scoped or automatic variables are contained in this segment.
Free space
Traditionally, the stack and heap were adjoined and grew towards each other. Once they met the free memory was exhausted. Today, with large address spaces and virtual memory, they are placed more freely.
Heap
Where dynamic memory allocation takes place (e.g. malloc(), realloc(), free()). In Unix-like OSes, the syscalls brk and sbrk control the amount of memory allocated to the heap.
BSS (Uninitialized data)
Contains global and static variables that are not explicitly initialized, or are initialized as 0. The compiler will initialize all data in this segment to 0. Historically, BSS stands for “block started by symbol”.
Data (Initialized data)
Contains global and static variables that are initialized by the programmer. Can be further classified into a read-only and read-write area.
Text
Contains the executable instructions, i.e. the code. Typically read-only and fixed size.

Examples §

BSS §

For example:

  • A global variable int blah; or a scoped variable static int foo; would be contained in this segment.
  • A global string char greeting[] = "hello world"; would be contained in the read-only area of this segment.
  • A global string char *greeting = "hello world"; would store the string literal "hello world" in the read-only area of this segment, and the char pointer variable in the read-write area.

Data §

For example:

  • A global variable int blah = 123; or a scoped variable static int foo = 456; would be contained in this segment.
  • Tagged
  • C