The memory layout of a C++ program is the way the operating system arranges a running process's address space into regions: the code (text) segment, initialised data, uninitialised data (BSS), the heap and the stack. Each region holds a different kind of data and has its own lifetime rules, which explains where variables live and why some bugs happen.
Knowing the memory layout c++ program authors rely on helps you reason about object lifetime, stack overflows, leaks and why static and new behave differently. The exact addresses are decided by the OS, so treat the picture below as a typical model, not a guarantee.
On a typical Linux or Windows process, from low to high addresses:
Modern systems also map shared libraries, memory-mapped files and thread stacks in between, and use address space layout randomisation (ASLR) so the addresses change on each run.
| Region | Contents | Lifetime | Managed by |
|---|---|---|---|
| Text | Compiled instructions, often read-only constants | Whole program | Loader |
| Data | Globals and statics with non-zero initial values | Whole program | Loader |
| BSS | Globals and statics initialised to zero or left default | Whole program | Loader |
| Heap | Dynamic allocations | Until freed | You and the allocator |
| Stack | Locals, parameters, return addresses | Until the function returns | Compiler-generated code |
C++ itself talks about storage duration (static, automatic, dynamic and thread), not segments. The segments are how common implementations realise those durations.
This program prints addresses of different kinds of objects so you can see the regions.
On a Linux x86-64 run you will usually see the code and data addresses close together and low, the heap somewhat higher, and the stack at the highest address. Casting a function pointer to void* is conditionally supported, which is fine on mainstream compilers. Do not rely on the numbers.
The stack is fast: allocating a local is just moving the stack pointer, and everything is released automatically when the function returns. It is small, typically a few megabytes per thread (8 MB is a common default on Linux, 1 MB on Windows), so very large arrays or deep recursion cause a stack overflow.
The heap is larger and flexible. Memory stays valid until you free it, but allocation is slower and can fragment.
A std::vector or std::string object itself is a small handle on the stack that owns a heap buffer. This is why returning containers by value is cheap and why they free their memory in the destructor.
Objects with static storage duration exist for the whole program. Globals are constructed before main and destroyed after it ends. The order of dynamic initialisation across different translation units is unspecified, which causes the "static initialisation order fiasco". Function-local statics (like counter above) are initialised the first time control reaches them, and that initialisation is thread-safe since C++11.
const char* or std::string.const global is usually placed in read-only data and may be folded into the code by the compiler.static local lives in data or BSS, not on the stack, so its value persists between calls.thread_local) get per-thread storage.new live on the heap, but the pointer variable that stores their address is a normal variable (often on the stack).You can look at the sections of a compiled binary with size or objdump.
The numbers vary with the program and the toolchain. To see a running process, read /proc/PID/maps on Linux, which lists each mapped region with its permissions (r-x for code, rw- for data, heap and stack).
No. It defines storage durations and the free store. Stack and heap are implementation details, though nearly all platforms use them.
Executable code is in the text segment. Virtual tables are read-only data created by the compiler, typically in a read-only data section.
Each call adds a stack frame. When the stack region is exhausted, the OS raises a fault, seen as a segmentation fault or a stack overflow error.
In C++ terms, new uses the free store and malloc uses the heap. Most implementations back both with the same allocator, but you must pair new with delete and malloc with free.
A C++ process has a code segment, data and BSS for static storage, a heap for dynamic allocation and a stack for automatic variables. The stack is fast, small and automatic, while the heap is large and needs an owner such as std::unique_ptr or std::vector. Know which region an object lives in and you can predict its lifetime, avoid stack overflows and leaks, and read memory-tool reports with confidence.