Memory Segmentation
The memory layout of a program shows how its data is stored in memory during execution. When a C program is executed, its memory is divided into several segments, each serving a distinct purpose: Memory is divided into sections such as code, data, heap, and stack.
- Data
- Text
! 100
the data segment can be sub-divided into:
- static data
- non-static data
! 100
static data can be grouped into:
- initialized
- uninitialized
! 120
dynamic data can be divided in 2:
- stack data (used for local variables)
- heap data (Dynamic memory allocated during program execution using functions like
malloc().)
! 120
Memory layout:

Text segment
Also called code segment, contains the instructions to execute a program
- fixed size
- read only
- stores the executable code of the program like program’s functions and instructions.
Data Segment (Dynamic / Static)
- 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.
B. Uninitialized Data Segment
.bss Segment: Contains global and static variables that are uninitialized or initialized to zero.
Example:
#include <stdio.h>
#include <stdlib.h>
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int b[20]; /* Uninitialized, so in the .bss segment */
int main()
{
;
}
- Array
a: Initialized with values, so it resides in the.datasegment; - Array
b: Uninitialized, so it resides in the.bsssegment;
! 400
Example 2: Initialised values
#include <stdio.h>
// Global variables (stored in initialized data segment)
int globalVar = 10;
char message[] = "Hello";
int main()
{
// Static variable (also stored in initialized data segment)
static int staticVar = 20;
printf("Global variable: %d\n", globalVar);
printf("Static variable: %d\n", staticVar);
printf("Message: %s\n", message);
return 0;
}
Output
Global variable: 10
Static variable: 20
Message: Hello
The above variables a and b will be stored in the Initialized Data Segment.
Example 3: uninitialised values
#include <stdio.h>
// Global uninitialized variables (stored in BSS segment)
int globalVar;
char message[50];
int main()
{
// Static uninitialized variable (also stored in BSS)
static int staticVar;
// Assigning values at runtime
globalVar = 10;
staticVar = 20;
snprintf(message, sizeof(message), "Hello BSS");
printf("Global variable: %d\n", globalVar);
printf("Static variable: %d\n", staticVar);
printf("Message: %s\n", message);
return 0;
}
- These variables are automatically initialized to zero by the system at runtime.
- It stores global and static variables that are not initialized by the programmer.
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().
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.
#include <stdio.h>
void func() {
// Stored in the stack
int local_var = 10;
}
int main() {
func();
return 0;
}
When the stack and heap meet, the program’s free memory is exhausted