Skip to content
Tutorials Center
My learning

Topics

Lesson 62 of 171 ยท Pointers, References and Memory

Memory Layout: Stack, Heap and Objects

See how C++ lays out memory - stack, heap and static storage, object size, alignment and padding, sizeof of classes, alignas, and why contiguous data is fast.

C++ lets you reason about memory in a way most languages hide. When you declare a variable, you can know where it lives, how many bytes it takes, how those bytes are arranged and when they are released. That knowledge is not trivia: it decides whether a program runs out of stack, why a struct is bigger than the sum of its members, and why one loop over the same data can be several times faster than another.

In this lesson you'll tour the regions of a running program (static memory, the stack and the heap), learn what sizeof and alignof really report, see how the compiler pads objects to keep members aligned, find out what a class costs in bytes, and get a first look at cache-friendly data. You'll use the pointers from Pointers and the heap from Dynamic Memory: new and delete, and the storage durations from Scope, Lifetime and Storage Duration.

The regions of a running program

When the operating system starts your program, it gives it a virtual address space. The C++ standard doesn't name regions, it only talks about storage durations, but every mainstream platform maps those durations onto the same few areas:

Region What lives there Lifetime Managed by
Code (text) Machine instructions of your functions Whole program Loader, read-only
Static data Globals, static variables, string literals Whole program Compiler and loader
Stack Local variables, function parameters, return addresses Until the enclosing block ends Automatic, per thread
Heap (free store) Objects from new, std::make_unique, container buffers Until you (or an owner) free them Allocator at run time

Thread-local variables (thread_local) get one copy per thread, usually stored in a small block next to each thread's stack.

The program below prints one address from each region. The exact numbers change on every run (operating systems randomize addresses to make attacks harder), but you'll see that the addresses fall into clearly separate ranges.

C++ One address from each memory region

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

On 64-bit Linux, code and static data usually sit close together at lower addresses, the heap follows them, and the stack sits near the top of the address space (addresses starting with 0x7ff...). Other systems arrange things differently. Never write code that depends on these ranges; the point is to build a mental model.

To print an address with std::println, convert the pointer to const void*. The formatter prints void pointers in hexadecimal, but it refuses other pointer types such as int* so that you don't print an address by accident. Function pointers need reinterpret_cast, because they don't convert to void* implicitly.

The stack

Every function call pushes a stack frame: a block that holds the function's parameters, its local variables, saved registers and the address to return to. When the function returns, the frame is popped simply by moving the stack pointer back. That is why stack allocation is nearly free: there is no search for free space and no bookkeeping, just one subtraction.

The stack has two properties you must respect:

  • It is small. A thread's stack is typically 8 MB on Linux and 1 MB on Windows by default. A local std::array<double, 1'000'000> (8 MB) can crash a program on its own. Large or variable-sized data belongs on the heap, usually inside a std::vector.
  • Locals die when their block ends. Returning a pointer or reference to a local leaves it dangling, as you saw in Returning Values.

On x86-64 and ARM64 the stack grows downward: each new frame sits at a lower address than its caller. You can watch it happen with a recursive function.

C++ Watching stack frames grow downward

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Each frame of dive takes a fixed number of bytes (the exact figure depends on the compiler, the optimization level and the platform). Deep recursion multiplies that number by the depth, which is how runaway recursion produces a stack overflow, a crash the program can't recover from. Recursion covers how to avoid it.

The heap

The heap (the standard calls it the free store) is a large pool managed by an allocator. new, std::make_unique, std::make_shared and every growing container ask the allocator for a block of a given size, and later hand it back. Heap memory is flexible: it can be huge, its size can be decided at run time, and its lifetime isn't tied to a scope. The price is speed and responsibility:

  • Each allocation must find a free block and record it, which costs far more than a stack bump (often tens to hundreds of nanoseconds).
  • Repeated allocation and freeing of different sizes can fragment the heap.
  • Someone must free each block exactly once. In modern C++ that someone is an owning object (a container or smart pointer), never you by hand.

A very common source of confusion: a std::vector or std::string variable is not on the heap. The small object itself (a few pointers) lives wherever you declared it, for example on the stack, and it points to a buffer on the heap.

C++ Small handles, big heap buffers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

With GCC's standard library a vector is 24 bytes (three pointers: begin, end and end of capacity) whether it holds zero elements or a million. A std::string is 32 bytes because it includes a 16-byte buffer for short strings, so a string of up to 15 characters needs no heap at all (the small string optimization). A unique_ptr is just one raw pointer, and a shared_ptr is two: one to the object and one to its control block. These numbers are not fixed by the standard; MSVC and libc++ use other layouts.

sizeof and alignof

sizeof(T) gives the number of bytes an object of type T occupies, including any padding. alignof(T) gives its alignment: the address of every T must be a multiple of that number. Processors read memory fastest (and on some architectures only) when a value sits at an address that is a multiple of its size, so each fundamental type usually has an alignment equal to its size.

C++ Sizes and alignments of fundamental types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

std::max_align_t is the type with the strictest ordinary alignment; new and malloc always return memory aligned at least that well (16 bytes on x86-64 Linux). Note that sizeof(char) is 1 by definition, and every other size is measured in multiples of it.

Padding: why structs grow

Members of a class are stored in declaration order, and each member must sit at an address that satisfies its alignment. When the next member needs a stricter alignment, the compiler inserts unused padding bytes in front of it. It may also add padding at the end, so that in an array every element starts properly aligned. The struct's own alignment is the largest alignment of any member.

C++ Member order changes the size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Careless and Tidy hold exactly the same data, yet Careless is 24 bytes and Tidy is 16: a third of every Careless is wasted padding. For one object that hardly matters. For a million objects in a vector, it is 8 MB of memory and cache bandwidth spent on nothing.

A simple rule keeps padding low: declare members from the largest alignment to the smallest (doubles and pointers, then ints, then shorts, then chars and bools). Don't let this override readability for small classes, but do apply it to types you store by the thousands.

offsetof (from <cstddef>) reports the byte offset of a member. It is only guaranteed to work on standard-layout types, roughly classes without virtual functions whose data members all have the same access level.

Never compare structs with std::memcmp or hash their raw bytes. Padding bytes have unspecified values, so two objects with equal members can still differ byte by byte. Compare members instead, for example with a defaulted operator==, which you'll meet in Operator Overloading.

Controlling alignment with alignas

alignas(N) asks for a stricter alignment than the type would get naturally. The main real-world uses are SIMD data (which wants 16-, 32- or 64-byte alignment) and keeping data used by different threads on separate cache lines.

C++
struct alignas(64) PerThreadCounter {   // each counter gets its own 64-byte cache line
    long long count = 0;
};
static_assert(sizeof(PerThreadCounter) == 64);
static_assert(alignof(PerThreadCounter) == 64);

You can't use alignas to make alignment weaker than natural. Compiler extensions like #pragma pack or __attribute__((packed)) remove padding, but they create misaligned members that are slow to access and can't safely be referenced by pointer. Reserve them for matching external binary formats, and prefer reading such formats byte by byte (see Binary Files).

What a class costs in bytes

Programmers coming from other languages often expect objects to carry hidden headers. In C++ a class costs exactly its data members plus padding, with a few precise exceptions:

  • Member functions take no space in objects. Their code exists once, in the code region, and they receive the object's address as the hidden this pointer.
  • Static data members take no space in objects. They live in static storage, once per program.
  • A class with virtual functions carries one hidden pointer (the vptr, 8 bytes on 64-bit systems) to a table of function addresses. You'll see why in Virtual Functions and Polymorphism.
  • An empty class has size 1, because two distinct objects must have distinct addresses.
  • A derived class contains its base class's members as a sub-object, followed by its own.
C++ sizeof of classes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The empty-class rule has an exception that libraries depend on. When an empty class is a base class, it may take zero bytes (the empty base optimization). C++20 adds [[no_unique_address]], which lets an empty member take zero bytes too. This is how a std::unique_ptr with a stateless custom deleter stays the size of a plain pointer.

C++
struct NoOpLogger {};                  // empty, stateless

struct Widget {
    int value = 0;
    [[no_unique_address]] NoOpLogger logger;   // takes no space with GCC and Clang
};
static_assert(sizeof(Widget) == sizeof(int));

(MSVC needs its own spelling, [[msvc::no_unique_address]], to get the same effect.)

Cache-friendliness: a first look

Modern CPUs are much faster than main memory. To hide the gap, they keep recently used memory in small, fast caches, and they move it in fixed blocks called cache lines, 64 bytes on most desktop and server CPUs. When you read one int, the 15 neighboring ints in the same line come along for free, and the hardware prefetcher starts fetching the following lines if you walk forward through memory.

The consequence: code that touches memory sequentially is fast, and code that jumps around is slow, even when both do the same amount of arithmetic. The program below sums the same matrix twice, once row by row (sequential in memory) and once column by column (jumping 16 KB between reads).

C++ Row order vs column order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The sums are identical, but column order is typically many times slower (more than ten times on our server), because almost every read misses the cache. Your exact timings depend on the machine and on what else it is running. The same effect is why a std::vector usually beats a std::list even for operations where the list has the better big-O, and why "an array of structs" vs "a struct of arrays" is a real design decision in games and simulations. Writing Fast C++ returns to these ideas with measurements.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. std::vector<int> v(1'000'000); is declared inside main. Where are the million ints stored?

    The vector object is a small handle in main's stack frame. Its elements live in a buffer the vector allocated on the heap, so even huge vectors don't overflow the stack.
  2. 2. With 8-byte doubles, what is sizeof of struct S { char a; double b; char c; }; on a typical 64-bit system?

    b must start at offset 8, so a is followed by 7 padding bytes. c sits at offset 16, and the struct is padded to a multiple of its alignment (8), giving 24.
  3. 3. Which change makes the struct from the previous question smaller without removing data?

    Ordering members from largest to smallest alignment lets the two chars share the tail padding, giving 16 bytes. alignas can't weaken alignment, and making b static changes the meaning of the data.
  4. 4. A class has two int members and ten non-virtual member functions. What is its sizeof on a typical system?

    Non-virtual member functions exist once in the code, not in each object, and C++ objects have no hidden header. Only a virtual function would add a hidden pointer.
  5. 5. Why does summing a row-major matrix column by column usually run slower than row by row?

    Both orders perform the same arithmetic. Row order walks memory sequentially and uses every byte of each 64-byte cache line; column order touches one int per line before jumping away.

Summary

  • A running program has code, static data, a stack per thread and a heap. Globals, static variables and literals live for the whole program; locals live on the stack until their block ends; heap objects live until their owner frees them.
  • Stack allocation is almost free but the stack is small (often 1โ€“8 MB). Put large or variable-sized data in containers, which store their elements on the heap.
  • std::vector and std::string objects are small handles (24 and 32 bytes with GCC); their elements live on the heap, except short strings kept inline by the small string optimization.
  • sizeof includes padding; alignof gives the required address multiple. Members are laid out in declaration order with padding to satisfy alignment.
  • Order members from largest to smallest alignment to reduce padding in types you store in bulk. Never compare objects with memcmp.
  • Objects cost their data members plus padding. Member functions and static members take no space; virtual functions add one hidden pointer; empty classes are 1 byte unless the empty base optimization or [[no_unique_address]] applies.
  • Sequential access through contiguous memory is fast because CPUs load 64-byte cache lines and prefetch ahead.

Next: Undefined Behavior and Memory Safety, where you'll learn what happens when code breaks the rules of memory, and the tools that catch it.