0
0
CConceptBeginner · 3 min read

Memory Layout of C Program: Explanation and Example

The memory layout of a C program is divided into sections like text (code), data (initialized variables), bss (uninitialized variables), heap (dynamic memory), and stack (function calls and local variables). These sections organize how the program's code and data are stored and accessed during execution.
⚙️

How It Works

Think of a C program's memory layout like a house with different rooms for specific purposes. The text segment is like the living room where the program's instructions (code) live. This area is read-only to keep the code safe from accidental changes.

The data segment stores variables that have been given initial values, like furniture already placed in the house. The bss segment holds uninitialized variables, similar to empty rooms waiting to be filled.

The heap is like a storage room where you can request extra space during runtime, useful for dynamic memory allocation. The stack is a workspace for temporary items like function calls and local variables, organized in a last-in, first-out manner, much like stacking plates.

💻

Example

This example shows different types of variables stored in various memory segments.

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

int global_var = 10;          // Stored in data segment
int uninit_global_var;        // Stored in bss segment

int main() {
    static int static_var = 20;   // Stored in data segment
    int local_var = 30;           // Stored in stack
    int *ptr = malloc(sizeof(int)); // Memory allocated on heap
    if (ptr == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }
    *ptr = 40;

    printf("Global initialized: %d\n", global_var);
    printf("Global uninitialized: %d\n", uninit_global_var);
    printf("Static variable: %d\n", static_var);
    printf("Local variable: %d\n", local_var);
    printf("Heap variable: %d\n", *ptr);

    free(ptr);
    return 0;
}
Output
Global initialized: 10 Global uninitialized: 0 Static variable: 20 Local variable: 30 Heap variable: 40
🎯

When to Use

Understanding the memory layout helps you write efficient and safe C programs. For example, knowing that local variables are on the stack helps avoid stack overflow by limiting large arrays there. Using the heap is essential when you need memory that lasts beyond a function call or when the size is not known at compile time.

It also aids debugging memory errors like accessing uninitialized variables or memory leaks by tracking where variables live in memory.

Key Points

  • Text segment: stores program code, read-only.
  • Data segment: stores initialized global and static variables.
  • BSS segment: stores uninitialized global and static variables, zero-initialized.
  • Heap: dynamic memory allocated at runtime.
  • Stack: stores local variables and function call info, grows and shrinks automatically.

Key Takeaways

The C program memory layout divides code and data into text, data, bss, heap, and stack segments.
Global and static variables live in data or bss segments depending on initialization.
Local variables are stored on the stack, which manages function calls.
Heap memory is used for dynamic allocation during program execution.
Knowing memory layout helps prevent bugs and optimize memory use.