C Programming - Beginner to Brilliance / Advanced Topics

Memory Map of a C Program

When you compile and run a C program, the program doesn’t just run "randomly" in your computer’s memory. It is carefully divided into well-defined sections in memory, each serving a specific purpose. Understanding this layout is crucial for systems programming, embedded C, debugging, and interviews.


🧠 Why Understand Memory Map?

Knowing how C manages memory helps you:

  • Avoid undefined behaviors and memory leaks

  • Write more efficient code

  • Understand pointer behaviors

  • Debug segmentation faults and stack overflows

  • Prepare for OS, Embedded & System-level programming


📜 Text Segment (Code Segment)

  • Contains compiled program instructions

  • Read-only to prevent code from being modified

  • Shared across processes (in OS)

int add(int a, int b) {
    return a + b;
}

This function’s machine code goes into the Text Segment.

🔒 It’s protected from writing. Trying to write to it = segmentation fault.


📥 Data Segment (Initialized Global & Static Variables)

  • Contains global/static variables that are explicitly initialized

  • Allocated when the program starts

  • Remains in memory until the program ends

int x = 5;          // Goes to Data Segment
static int count = 0; // Also in Data Segment

✅ Value already defined before main() begins execution.


📦 BSS Segment (Uninitialized Global & Static Variables)

  • Stores global/static variables that are declared but not initialized

  • Automatically initialized to zero by the OS

int a;           // Stored in BSS
static int b;    // Also in BSS

❗ Difference from Data Segment: No initial value specified.


🧠 Heap Segment

  • Dynamic memory region used with malloc(), calloc(), realloc(), free()

  • Grows upward (towards higher addresses)

  • Managed manually by the programmer

int *arr = (int*)malloc(5 * sizeof(int));

🔁 You allocate and free memory on your own. Memory leak? Happens here!


🧭 Stack Segment

  • Used for:

    • Function call/return context

    • Local variables

    • Function parameters

  • Grows downward (towards lower addresses)

void foo() {
    int y = 10; // Local variable → stored on stack
}

🧨 If too many function calls are made recursively, you’ll get a stack overflow.


🌱 Environment and Arguments

  • Stores:

    • argc, argv[]: command-line arguments

    • Environment variables like PATH, HOME, etc.

This part is managed by the OS when the program starts.


🧩 Example Program with Variable Mapping

#include <stdio.h>
#include <stdlib.h>

int g = 5;         // Data Segment
static int s;      // BSS Segment

int main() {
    int a = 10;        // Stack
    int *p = malloc(5 * sizeof(int));  // Heap
    static int k = 20; // Data Segment

    printf("Hello C");
    return 0;
}

Variable

Segment

g

Data

s

BSS

k

Data

a

Stack

p

Stack (pointer), Heap (data)


🛠️ Key Memory Behavior Insights

Memory Segment

Lifetime

Access Speed

Common Issues

Stack

Short

Fast

Stack overflow

Heap

Manual

Medium

Memory leaks, double free

Data/BSS

Long

Fast

None (managed by system)

Text

Static

Fast

Cannot modify