Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

Memory model

Understand the abstract machine contract between your source code, the optimizer, and hardware store buffers.

NotebookLM
[intro.memory][intro.races][atomics.order][atomics.fences]

Release/acquire does not flush CPU caches—it builds a happens-before bridge so ordinary writes before release become visible after acquire.

CppCon angle
Show that compiler reordering and CPU store buffers exist independently; memory_order constrains BOTH the compiler optimizer and hardware fence instructions emitted.
Common audience trap
Assuming memory_order_seq_cst is required on every atomic flag, or conversely thinking memory_order_relaxed prevents adjacent non-atomic writes from reordering across it.
Stage takeaway
A single unsynchronized data race on a non-atomic variable poisons the entire execution with Undefined Behavior—even if the hardware reads an intact word.

Memory locations & the bit-field trap

[intro.memory] §6.7.1

Before C++11, C++ had no standard notion of threads; concurrency lived inside POSIX or Win32 headers while compilers happily hoisted loads out of loops. C++11 introduced the abstract memory model.

Crucially, two adjacent scalar struct members (like int a; int b;) are separate memory locations and can be mutated concurrently by two threads. Adjacent non-zero-width bit-fields (int b : 5; int c : 11;), however, share ONE memory location—concurrently mutating b and c on different threads is a silent data race!

Zero-width bit-field separator ([intro.memory])
struct Packed {
    unsigned a : 4;
    unsigned   : 0; // Forces next bit-field into a new memory location
    unsigned b : 4;
};
// Thread 1 mutating p.a and Thread 2 mutating p.b is now race-free.

Key rule: Bit-fields inside the same sequence share a single read-modify-write word and therefore share one C++ memory location.

Sequenced-before, synchronizes-with, happens-before

[intro.races] & [atomics.order]

Within one thread, evaluation order gives sequenced-before. Across threads, an atomic store with memory_order_release that is read by an atomic load with memory_order_acquire establishes a synchronizes-with edge.

Transitively chaining sequenced-before and synchronizes-with gives happens-before. Notice what memory_order_relaxed lacks: relaxed atomics avoid data-race UB on the atomic variable itself and obey single-variable modification order, yet establish zero happens-before ordering for surrounding non-atomic data.

Classic publication via acquire/release
int payload = 0;
std::atomic<bool> ready{false};

// Thread 1 (Producer)
payload = 42;                                  // (1) sequenced-before (2)
ready.store(true, std::memory_order_release);  // (2) synchronizes-with (3)

// Thread 2 (Consumer)
while (!ready.load(std::memory_order_acquire)) {} // (3)
assert(payload == 42);                            // Guaranteed! (1) happens-before (4)

Key rule: Release prevents prior memory accesses from moving after the store; acquire prevents subsequent memory accesses from moving before the load.

Across revisions (C++11 → C++20)

C++11

Defined the abstract memory model, std::atomic<T>, 6 memory orders, and formal data-race Undefined Behavior.

C++20

Added std::atomic_ref<T>, wait/notify on atomics, and discouraged memory_order_consume in favor of acquire until hardware dependency tracking matures.

Full 1:1 cppreference specification

Original specification mirrored from cppreference.com with restyled declaration panels and code windows.

Defines the semantics of computer memory storage for the purpose of the C++ abstract machine.

The memory available to a C++ program is one or more contiguous sequences of bytes. Each byte in memory has a unique address.

Byte

A byte is the smallest addressable unit of memory. It is defined as a contiguous sequence of bits, large enough to hold

  • the value of any UTF-8 code unit (256 distinct values) and of
(until C++23)
(since C++23)

Similar to C, C++ supports bytes of sizes 8 bits and greater.

The types char, unsigned char, and signed char use one byte for both storage and value representation. The number of bits in a byte is accessible as CHAR_BIT or std::numeric_limits<unsigned char>::digits.

Memory location

A memory location is the storage occupied by the object representation of either an object of scalar type that is not a bit-field, or the largest contiguous sequence of bit-fields of non-zero length.

Note: Various features of the language, such as references and virtual functions, might involve additional memory locations that are not accessible to programs but are managed by the implementation.

snippet.cpp
struct S
{
    char a;     // memory location #1
    int b : 5;  // memory location #2
    int c : 11, // memory location #2 (continued)
          : 0,
        d : 8;  // memory location #3
    struct
    {
        int ee : 8; // memory location #4
    } e;
} obj; // The object “obj” consists of 4 separate memory locations

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1953 C++98 objects occupying the same storage were
considered as different memory locations
memory location
now refers to storage

See also

C documentation for Memory model

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.