Circuit Background

C Memory Layout & Storage Classes

Master the low-level mechanics of C. Visualize how variables live, die, and where they reside in your computer's RAM.

The Big Picture

When you run a C program, the operating system assigns a block of RAM to it. This memory is strictly organized into segments. Understanding this layout is crucial for debugging segmentation faults, memory leaks, and understanding variable lifetimes.

  • check_circle
    Storage Classes define where a variable is stored (Stack, Heap, Data) and how long it survives.
  • check_circle
    Stack vs. Heap is the most critical distinction. The Stack is automatic and organized; the Heap is manual and flexible (but dangerous).

Memory Segment Hierarchy

High Address 0xFFFFFFFF
STACK Local variables, Function calls
Free Memory (Gap)
HEAP Dynamic (malloc/free)
BSS & DATA Globals & Statics
TEXT (Code) Instructions (Read-Only)
Low Address 0x00000000
Interactive Demo

Code Execution & Memory Visualizer

Step through a C program to see how memory is allocated in real-time.

code main.c
#include
#include
int global_var = 42;
int global_uninit;
void func() {
static int s_count = 0;
int local_a = 10;
s_count++;
}
int main() {
int *ptr;
ptr = malloc(sizeof(int));
*ptr = 99;
func();
free(ptr);
return 0;
}
> Simulation reset. Ready to start.

RAM Snapshot

STACK (Local Vars)
Stack Grows Down ↓
↑ Heap Grows Up
HEAP (Dynamic Memory)
DATA / BSS (Globals & Statics)
TEXT SEGMENT (Code)

Deep Dive: Storage Classes

autorenew The Default: auto

This is the default for all variables declared inside a block. You rarely type the keyword auto explicitly. They are born when execution enters the block and die when it leaves.

Location
Stack
Scope
Local (Block)
Lifetime
Function Call
Default Value
Garbage
hover lines for info
void myFunction() {
auto int x = 10;
int y;
if (x > 5) {
int z = 5;
}
}

Created by Rajat Kumar

in Connect on LinkedIn