memory_layout.png

Stack

The stack is used for managing function calls and local variables.

Local variables, function parameters, and return addresses are stored here.

Automatically managed by the operating system.

Data structure: LIFO

Heap

The heap is used for dynamic memory allocation.

Memory allocation is not automatically managed by the OS, and is larger than the stack. It is the programmer’s responsibility to allocate and deallocate memory.

Uninitialized data (BSS)

This segment holds global and static variables that are declared but not initialized by the programmer.

Example:

static int x;

Without an explicit value, it will be stored in the BSS segment.

Initialized data (Data segment)

This segments holds global and static variables explicitly initialized by the programmer

Example

static int x = 1;

Memory is allocated for these variables at the start of the program and persists until the program terminates.

Text segment

The text segment contains executable code of the program. It is where instructions for the CPU is stored.

Typically read-only to prevent accidental modification of instructions during execution.