Understanding how memory is organized in a C program makes a big difference when writing efficient and reliable code. It explains where variables are stored, how long they exist, and how memory is managed during execution.
Table of Contents
What is Memory Layout in C?
When a C program runs, the operating system divides memory into multiple sections. Each section has a clearly defined role, which helps the program execute in an organized and predictable way.
Key Characteristics of Memory Layout
- Structured Segmentation: Memory is divided into clearly defined sections such as code, data, heap, and stack.
- Different Allocation Types: Some memory is allocated at compile time, others at runtime.
- Variable Lifetime Control: Each segment determines how long variables exist.
- Dynamic Growth Behavior: Stack and heap can grow/shrink during execution.
- Performance and Safety Impact: Helps prevent memory leaks and crashes.
Main Sections
- Code Segment: The code segment stores program instructions.
- Data Segment: The data segment stores global and static variables.
- Heap Segment: The heap segment is used for dynamic memory allocation.
- Stack Segment: The stack segment stores local variables and function calls.

Code Segment (Text Segment)
The Code Segment, also called the Text Segment, is the part of memory where all executable instructions of a program are stored after compilation. The CPU reads instructions from this section and executes them one by one during program execution. Since it contains the actual program code, this segment is very important for controlling the flow of execution.
- Storage of Instructions: The code segment stores machine-level executable instructions generated by the compiler. These instructions tell the CPU what operations need to be performed.
- Read-Only Nature: In most systems, the code segment is read-only. This prevents accidental modification of program instructions during execution and improves security.
- Shared Memory: Multiple instances of the same program can share the same code segment in memory. This helps reduce memory usage because the operating system does not need to load duplicate copies of the code.
- Fixed Size: The size of the code segment is usually determined at compile time. It generally remains unchanged while the program is running.
- Execution Control: The CPU fetches instructions directly from the code segment. Every statement written in the program is converted into instructions stored here.
int main() {
printf("Hello World");
return 0;
}
Explanation:
In this example, the compiled instructions for main(), printf(), and return 0 are stored inside the code segment.
Data Segment
The Data Segment stores global and static variables used in a program. Unlike local variables, these variables exist throughout the execution of the program.
- Stores Global Variables: Variables declared outside functions are stored in the data segment.
- Stores Static Variables: Static variables retain their values throughout the program execution, so they are also stored here.
- Divided into Two Parts: The data segment is divided into two parts:
- Initialized Data Segment: Stores variables with assigned initial values.
- Uninitialized Data Segment (BSS): Stores variables declared without initialization.
- Accessible Throughout Execution: Variables in the data segment remain in memory until the program terminates.
- Used for Persistent Storage: Since these variables are not destroyed after function calls, they help maintain data during program execution.
#include stdio.h
// Initialized global variable
int globalVar = 10;
// Uninitialized static variable
static int count;
int main()
{
printf("%d", globalVar);
return 0;
}
Here:
- globalVar is stored in the initialized data segment.
- count is stored in the BSS section because it is not initialized.
Heap Segment
The Heap Segment is the part of memory used for dynamic memory allocation during program execution. Memory in the heap is allocated and deallocated manually by the programmer using functions like malloc(), calloc(), realloc(), and free(). It is mainly used when the size of data is not known at compile time.
- Dynamic Allocation: Memory is allocated at runtime according to the program’s requirements. This makes the heap useful for flexible memory usage.
- Manual Management: The programmer controls heap memory using functions such as malloc() and free(). If memory is not freed properly, it remains occupied.
- Flexible Size: The heap can grow or shrink during execution depending on memory allocation needs.
- Slower Access: Accessing heap memory is slower compared to stack memory because allocation and deallocation involve additional overhead.
- Risk of Memory Leaks: If allocated memory is not released using free(), memory leaks can occur, reducing available memory.
#include stdio.h
#include stdlib.h
int main()
{
int ptr = (int) malloc(sizeof(int));
*ptr = 25;
printf("Value = %d", *ptr);
free(ptr);
return 0;
}
Explanation:
- malloc(sizeof(int)) allocates memory dynamically from the heap.
- ptr stores the address of the allocated memory.
- free(ptr) releases the memory after use.
Stack Segment
TThe Stack Segment stores local variables, function parameters, and function call information. It works in a Last In First Out (LIFO) manner, meaning the most recently added data is removed first.
- Storage of Local Variables: Variables declared inside functions are stored in the stack memory.
- Automatic Memory Management: Memory in the stack is automatically allocated and deallocated when functions are called and completed.
- Fast Access: Stack operations are very fast because memory allocation happens in a fixed order.
- Limited Size: The stack size is smaller than the heap, so very large data cannot usually be stored here.
- Risk of Stack Overflow: Excessive recursion or very large local variables can exhaust stack memory and crash the program.
#include stdio.h
void display()
{
// Stored in stack
int num = 10;
printf("%d", num);
}
int main()
{
display();
return 0;
}
Explanation:
- The variable num is created when display() is called.
- After the function finishes execution, the variable is automatically removed from the stack.
- Function calls and return addresses are also managed through the stack.
Stack vs Heap
| Feature | Heap Segment | Stack Segment |
|---|---|---|
| Memory Allocation | Allocated dynamically at runtime | Allocated automatically |
| Management | Manual using malloc() and free() | Managed automatically |
| Speed | Slower access | Faster access |
| Size | Larger memory area | Smaller memory area |
| Main Usage | Dynamic data storage | Local variables and function calls |
| Risk | Memory leaks | Stack overflow |

Why Memory Layout Matters?
Understanding memory layout in C is important because it helps programmers know how data and instructions are stored and managed inside computer memory.- Easier Debugging: Knowledge of memory layout helps programmers identify issues like segmentation faults, invalid memory access, and stack overflow errors more easily.
- Better Memory Usage: Understanding how memory works allows efficient allocation and deallocation of memory, reducing memory wastage and leaks.
- Improved Performance: Choosing the correct memory segment for variables and data can improve execution speed and overall program performance.
- Safer Code: Proper memory handling prevents crashes, buffer overflows, and other runtime errors that may make programs unstable.
- Strong Foundation for Advanced Concepts: Topics such as pointers, dynamic memory allocation, recursion, operating systems, and data structures require a good understanding of memory layout.
Conclusion
Memory layout in C defines how a program’s instructions and data are organized in memory during execution. Different segments such as the code segment, data segment, heap segment, and stack segment each have specific purposes and behaviors. Understanding these memory sections helps programmers manage memory efficiently, improve program performance, and avoid common errors like memory leaks and stack overflow. A strong understanding of memory layout also builds the foundation for advanced programming concepts in C.
Frequently Asked Questions
1. Which memory segment stores executable code?
The Code Segment stores executable program instructions.
2. Where are local variables stored in C?
Local variables are usually stored in the Stack Segment.
3. Why is heap memory used?
Heap memory is used for dynamic memory allocation at runtime.
4. What causes stack overflow?
Deep recursion or very large local variables can cause stack overflow.
5. Which functions are used for dynamic memory allocation?
Functions like malloc(), calloc(), realloc(), and free() are used for dynamic memory management in C.
0 Comments