Memory Layout in C (Stack, Heap, Code, Static Area)
Every C program is stored in a well-defined memory layout divided into multiple sections.
Understanding memory layout helps in debugging, writing efficient programs, and avoiding errors such as segmentation faults.
What is Memory Layout?
When a C program runs, the OS divides memory into separate segments. Each section has a fixed role, such as storing code, global variables, function calls, and dynamically allocated memory.
- Controls where variables are stored
- Affects program performance
- Important for pointers & dynamic memory
- Crucial for understanding memory leaks
Major Sections of C Program Memory
- 1. Code/Text Segment
- 2. Data Segment (Static Global Area)
- 3. BSS Segment (Uninitialized Globals)
- 4. Stack
- 5. Heap
1. Code (Text) Segment
This section stores:
- Compiled machine instructions
- Function definitions
- Readonly constants like string literals
âš The code section is read-only to prevent modification of instructions during execution.
Example: "Hello" in printf("Hello") is stored in the Code/Text segment.
2. Data Segment (Static + Global Variables)
This section stores:
- Initialized global variables
- Initialized static variables
Lifetime: Entire execution of program.
int a = 10; // Stored in data segment static int x=5; // Also stored in data segment
3. BSS Segment (Uninitialized Data)
Stores:
- Uninitialized global variables
- Uninitialized static variables
Default value: 0
int count; // Goes to BSS static int value; // Also BSS
4. Stack Segment
- Function call frames
- Local variables
- Function arguments
- Return addresses
Automatically managed by program
âš Stack grows and shrinks automatically, but it has limited size.
Stack overflow happens if recursion is too deep.
void test(){
int x = 10; // Stored in stack
}
5. Heap Segment
- Used for dynamic memory allocation
- Created using malloc(), calloc(), realloc()
- Must be freed manually using free()
Lifetime: until programmer frees it.
int *p = malloc(sizeof(int)); *p = 50; // Stored in heap
âš Not freeing heap memory leads to memory leaks.
Diagram of C Memory Layout
+------------------------+ | Command-Line Args | +------------------------+ | Stack | | (Local variables) | +------------------------+ | Heap | | (malloc/free area) | +------------------------+ | BSS Segment | | (Uninitialized global) | +------------------------+ | Data Segment | | (Initialized global) | +------------------------+ | Text Segment | | (Program instructions) | +------------------------+
Real-Life Uses of Memory Layout
- Understanding segmentation faults
- Debugging pointer issues
- Avoiding memory leaks
- Writing optimized and safe C programs
- Knowledge for interviews & OS internals
Practice Questions
- What is the difference between Stack and Heap?
- Where are global variables stored?
- Explain BSS segment with example.
- What happens if you don't free dynamic memory?
- Why is the text segment read-only?
Memory Layout in C — 10 Practical Examples
1. Stack: Local Variable in Function
#include <stdio.h>
void func() {
int x = 10; // stored in stack
printf("%d", x);
}
int main() {
func();
return 0;
}
2. Stack Frame Changes per Function Call
#include <stdio.h>
void test(int a) { // 'a' lives in stack frame
int b = a + 5;
printf("%d ", b);
}
int main() {
test(10);
test(20);
return 0;
}
3. Heap: malloc() Allocation
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int)); // heap
*p = 50;
printf("%d", *p);
free(p);
return 0;
}
4. Heap vs Stack Difference
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 10; // stack
int *p = malloc(4); // heap
*p = 99;
printf("%d %d", a, *p);
free(p);
return 0;
}
5. Static Area: static Variable Retains Value
#include <stdio.h>
void counter() {
static int c = 0; // static area
c++;
printf("%d ", c);
}
int main() {
counter();
counter();
counter();
return 0;
}
6. Global Variable in Data Segment
#include <stdio.h>
int x = 100; // global → static/global area
int main() {
printf("%d", x);
return 0;
}
7. Code Segment Example (Function Stored in Text Segment)
#include <stdio.h>
void greet() { // function stored in code/text segment
printf("Hello\n");
}
int main() {
greet();
return 0;
}
8. Dangling Pointer After Free (Heap)
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = malloc(sizeof(int));
*p = 5;
free(p); // memory freed
// p is dangling pointer now
return 0;
}
9. Local Array Stored in Stack
#include <stdio.h>
int main() {
int arr[5] = {1,2,3,4,5}; // stack
printf("%d", arr[2]);
return 0;
}
10. Memory Mix: Code, Stack, Heap, Static Area
#include <stdio.h>
#include <stdlib.h>
int g = 10; // static/global area
void show() { // code/text segment
static int s = 5; // static area
int x = 1; // stack
int *h = malloc(4); // heap
*h = x + s + g;
printf("%d", *h);
free(h);
}
int main() {
show();
return 0;
}
Practice Task
Write a C program that uses:
✔ Global variables
✔ Static variables
✔ Local variables
✔ Dynamic memory
Print their values and explain in which memory segment each is stored.