VOOZH about

URL: https://www.geeksforgeeks.org/c/memory-layout-of-c-program/

⇱ Memory Layout of C Programs - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Memory Layout of C Programs

Last Updated : 8 Apr, 2026

The memory layout of a program shows how its data is stored in memory during execution. It helps developers understand and manage memory efficiently.

  • Memory is divided into sections such as code, data, heap, and stack.
  • Knowing the memory layout is useful for optimizing performance, debugging and prevent errors like segmentation fault and memory leak.
👁 Memory-Layout-of-C-Program

1. Text Segment

  • The text segment (or code segment) stores the executable code of the program like program’s functions and instructions.
  • The segment is usually read-only to prevent accidental modification during execution.
  • It is typically stored in the lower part of memory.
  • The size of the text segment depends on the number of instructions and the program’s complexity.

2. Data Segment

  • The data segment stores global and static variables of the program.
  • Variables in this segment retain their values throughout program execution.
  • The size of the data segment depends on the number and type of global/static variables.
  • It is divided into initialized and uninitialized (BSS) sections.

A. Initialized Data Segment

As the name suggests, it is the part of the data segment that contains global and static variables that have been initialized by the programmer.


Output
Global variable: 10
Static variable: 20
Message: Hello

The above variables a and b will be stored in the Initialized Data Segment.

B. Uninitialized Data Segment (BSS)

  • The uninitialized data segment is often called the BSS segment.
  • It stores global and static variables that are not initialized by the programmer.
  • These variables are automatically initialized to zero by the system at runtime.

Output
Global variable: 10
Static variable: 20
Message: Hello BSS

3. Heap Segment

  • The heap segment is used for dynamic memory allocation.
  • It starts at the end of the BSS segment and grows towards higher memory addresses.
  • Memory in the heap is managed using functions like malloc(), realloc(), and free().
  • The heap is shared by all shared libraries and dynamically loaded modules in a process.

4. Stack Segment

  • The stack stores local variables, function parameters, and return addresses for each function call.
  • Each function call creates a stack frame in this segment.
  • The stack is usually at higher memory addresses and grows opposite to the heap.
  • When the stack and heap meet, the program’s free memory is exhausted.

Practical Examples

The size(1) command in MinGW reports the sizes (in bytes) of the text, data, and bss segments of a binary file.

1. Check the following simple C program 

Memory Layout

gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 248 8 1216 4c0 memory-layout

2. Let us add one global variable in the program, now check the size of bss

Memory Layout

gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 248 12 1220 4c4 memory-layout

3. Let us add one static variable which is also stored in bss.

Memory Layout

gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 248 16 1224 4c8 memory-layout

4. Let us initialize the static variable which will then be stored in the Data Segment (DS)

Memory Layout

gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 252 12 1224 4c8 memory-layout

5. Let us initialize the global variable which will then be stored in the Data Segment (DS)

Memory Layout

gcc memory-layout.c -o memory-layout
size memory-layout
text data bss dec hex filename
960 256 8 1224 4c8 memory-layout

Example to Verify the Memory Layout


Output
Address of foo:		0x4011d0
Address of cgvar:	0x402084
Address of gvar:	0x404020
Address of ugvar:	0x404028
Address of hvar:	0x119592a0
Address of lvar:	0x7ffe8289c66c

Comparing above addresses, we can see than it roughly matches the memory layout discussed above.

Comment