Memory Layout in Linux
In Linux systems, process memory is divided into two major regions:
User applications cannot directly access kernel space. Attempting to access restricted memory causes a segmentation fault.
Inside user space, a C program memory layout is mainly divided into four important segments:
- Text Segment
- Data Segment
- Heap Segment
- Stack Segment

1. Text Segment (Code Segment)
The Text Segment is also called the Code Segment. It stores the compiled machine instructions of the program.
Features of Text Segment
- Read-only memory region
- Contains executable instructions
- Shared among multiple instances of the same program
- Helps save system memory
The operating system protects this section from modification to improve security and stability.
Example
#include
int main() {
printf("Hello World");
return 0;
}
The compiled instructions of main() are stored inside the text segment.

2. Data Segment
The Data Segment stores global variables and static variables.
It is divided into two parts:
Initialized Data Segment
Stores variables initialized by the programmer.
Example
int count = 10;
static int value = 5;
These variables occupy memory before program execution starts.
BSS Segment (Block Started by Symbol)
The BSS segment in C stores uninitialized global and static variables.
Example
int number;
static int total;
The operating system automatically initializes these variables to:
This reduces executable file size because only variable metadata is stored.

3. Heap Segment
The Heap is used for dynamic memory allocation in C.
Functions such as:
- malloc()
- calloc()
- realloc()
- free()
operate on heap memory.
The heap grows upward during runtime as memory requests increase.
Example of Heap Memory Allocation
#include
#include
int main() {
int *ptr;
ptr = (int*)malloc(sizeof(int));
*ptr = 100;
printf("%d", *ptr);
free(ptr);
return 0;
}
Important Characteristics
- Runtime memory allocation
- Flexible memory size
- Memory must be manually released using free()
- Improper handling causes memory leaks
Heap Memory Advantages
- Dynamic memory allocation
- Useful for linked lists, trees, queues, and graphs
- Efficient for variable-sized data structures
Heap Memory Disadvantages
- Slower than stack memory
- Memory fragmentation may occur
- Risk of memory leaks
4. Stack Segment
The Stack Segment stores:
- Local variables
- Function parameters
- Return addresses
- Function call information
The stack grows downward in memory.
Every function call creates a separate memory block called a stack frame.
Stack Frame in C
Whenever a function is called:
- A new stack frame is pushed onto the stack
- Local variables are created
- Function arguments are stored
- Return address is saved
When the function finishes execution, the stack frame is automatically removed.
Example
#include
void display() {
int x = 10;
printf("%d", x);
}
int main() {
display();
return 0;
}
Here:
- x is stored inside the stack frame of display()
- After function execution, memory is automatically released
Stack vs Heap in C Programming
| Feature | Stack | Heap |
|---|
| Memory Allocation | Automatic | Manual |
| Speed | Faster | Slower |
| Size | Limited | Large |
| Managed By | Compiler | Programmer |
| Storage | Local Variables | Dynamic Data |
| Deallocation | Automatic | Using free() |
Memory Layout Diagram in C
The overall memory organization of a C program looks like this:
---------------------
| Stack |
|-------------------|
| |
| Heap |
|-------------------|
| BSS Segment |
|-------------------|
| Initialized Data |
|-------------------|
| Text Segment |
---------------------
Common Memory Errors in C
1. Memory Leak
Occurs when dynamically allocated memory is not freed.
ptr = malloc(100);
// forgot free(ptr);
2. Stack Overflow
Occurs when excessive recursion or large local variables consume stack memory.
3. Segmentation Fault
Occurs when accessing invalid memory locations.
Example:
int *ptr = NULL;
*ptr = 10;
Importance of Memory Layout in Embedded Systems
In embedded C programming, memory resources are limited. Understanding memory segments helps developers:
- Optimize RAM usage
- Improve execution speed
- Reduce power consumption
- Avoid crashes in microcontrollers
This concept is especially important in:
Best Practices for Memory Management in C
- Always initialize pointers
- Free dynamically allocated memory
- Avoid unnecessary global variables
- Use recursion carefully
- Monitor stack usage in embedded systems
- Check malloc return values
Conclusion
Understanding memory layout in C programming is fundamental for writing efficient, optimized, and secure applications. The text segment, data segment, heap, and stack each play a critical role in program execution.
A solid understanding of:
- stack and heap memory in C
- Linux process memory layout
- dynamic memory allocation
- stack frames
- BSS and data segments
helps programmers build better software and avoid common memory-related issues.
For embedded systems, IoT development, and system-level programming, mastering memory management is essential for creating reliable applications.
