Memory Layout in C Programming Language

Memory Layout in C


Memory layout of C program in Unix

tags: Unix  reading  Unix  

C program is divided into code segment (text segment), initialized data segment (initialized data segment), uninitialized data segment (uninitialized data segment), stack (stack) and heap (heap) in the process. Its memory distribution is shown in Figure 1

Figure 1. The memory layout of    C program is running

  • Code snippets are often read-only content, providing necessary steps for the process to run.
  • The initialization data fragment contains the variable values ​​that are explicitly initialized in the C code.
  • The uninitialized data segment (also known as the bss segment) contains variables that are not explicitly initialized in the C code. The data in this segment is reset to 0 when the program is started (all bits are filled with 0).
  • The stack is used to store automatic local variables (automatic variables) and function call information when the program is running. Since the function call stack appears to grow the function chain to a lower address, when a recursive call occurs, the function chain is elongated, and the automatic local variable is stored as a new variable. Therefore, the recursive call generates a