# Memory model

> C++ Atlas: `cppatlas.com/w/cpp/language/memory_model` | Source: https://en.cppreference.com/w/cpp/language/memory_model | Domain: Language | Standards: C++23

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

## Executive Mental Model (Study Companion)

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

- **Standard Era:** C++11 → C++20
- **Primary ISO Clauses:** [intro.memory], [intro.races], [atomics.order], [atomics.fences]
- **CppCon Talk 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 Map Walkthrough: Multi-Core Litmus Test & Synchronization Simulator

In the C++ abstract machine, two threads accessing the same memory location where at least one is a non-atomic write and neither happens-before the other creates a data race, which is Undefined Behavior. To establish inter-thread ordering without heavy mutexes, a release store on one thread pairs with an acquire load reading that stored value on another thread, forming a synchronizes-with edge. Everything sequenced before the release store now happens-before everything sequenced after the acquire load. However, store buffering—where two threads write to separate atomics and read each other's atomics—can still observe both reads returning zero under acquire/release because store buffers delay visibility; preventing that requires memory_order_seq_cst.

### 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]):*
```cpp
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 takeaway:** 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:*
```cpp
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 takeaway:** Release prevents prior memory accesses from moving after the store; acquire prevents subsequent memory accesses from moving before the load.

## Official Specification Summary (cppreference)

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):

- any member of the basic execution character set.

(since C++23):

- the ordinary literal encoding of any element of the basic literal character set.

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.

```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](https://en.cppreference.com/w/c/language/memory_model)

## Podcast Host Debate & Socratic Standard Traps

Use the prompts and tricky ISO standard questions below to guide a Socratic audio discussion:

- Explain why 'flushing CPU caches' is a misleading mental model for acquire/release, and reframe it around store buffers and happens-before edges.
- Walk listeners through the Store Buffering litmus test where acquire/release still permits both threads to read zero.
- Compare bit-field sharing in [intro.memory] and why volatile is never a replacement for std::atomic.

### Trap 1: [intro.memory] §6.7.1 (standard-lawyer)

In struct S { int x : 4; int y : 4; };, Thread 1 writes s.x = 1 and Thread 2 concurrently writes s.y = 2. What does the C++ standard say?

```cpp
struct S { int x : 4; int y : 4; } s{};
// Thread 1: s.x = 1;
// Thread 2: s.y = 2;
```

A. Well-defined because x and y are distinct member names
B. Undefined behavior due to a data race on a shared memory location
C. Well-defined on 64-bit architectures only
D. Implementation-defined torn read

**Standard Answer (B):** Undefined behavior due to a data race on a shared memory location

**Why:** Contiguous non-zero-length bit-fields belong to the same memory location ([intro.memory]). Compilers compile s.x = 1 as a non-atomic read-modify-write of the containing word, racing directly with Thread 2.

### Trap 2: [atomics.order] §33.5.4 (cppcon-stage)

Two atomics x and y start at 0. Thread 1 runs x.store(1, release); r1 = y.load(acquire); and Thread 2 runs y.store(1, release); r2 = x.load(acquire);. Can BOTH r1 == 0 and r2 == 0 happen?

```cpp
std::atomic<int> x{0}, y{0};
// T1: x.store(1, std::memory_order_release); int r1 = y.load(std::memory_order_acquire);
// T2: y.store(1, std::memory_order_release); int r2 = x.load(std::memory_order_acquire);
```

A. No—acquire/release forces a single global order across x and y
B. Yes—store buffers allow each core's acquire load to bypass its own prior release store
C. No—only memory_order_relaxed allows r1 == 0 && r2 == 0
D. Undefined behavior because x and y are accessed without a mutex

**Standard Answer (B):** Yes—store buffers allow each core's acquire load to bypass its own prior release store

**Why:** Acquire/release establishes pairwise happens-before ONLY when an acquire load actually reads a value written by a release store on the SAME atomic. Neither release prevents a following acquire load from reordering ahead of it (even on x86-64 TSO!). Only memory_order_seq_cst provides a single total order preventing r1 == 0 && r2 == 0.

*CppCon speaker note:* Classic Store Buffering (SB) litmus test—crucial for showing why acquire/release is NOT sequentially consistent.

### Trap 3: [atomics.order] (subtle)

Thread 1 executes data = 42; flag.store(true, relaxed); while Thread 2 loops until flag.load(relaxed) and reads data. Why can assert(data == 42) fail?

```cpp
// T1: data = 42; flag.store(true, std::memory_order_relaxed);
// T2: while (!flag.load(std::memory_order_relaxed)) {}
//     assert(data == 42);
```

A. Only on ARM; x86 forbids this bug
B. There is a data race on data (Undefined Behavior) because relaxed creates no synchronizes-with edge
C. flag itself can tear and read partially written bits
D. The while loop is an infinite loop unless volatile is added

**Standard Answer (B):** There is a data race on data (Undefined Behavior) because relaxed creates no synchronizes-with edge

**Why:** memory_order_relaxed prevents a data race ONLY on flag itself. It establishes zero happens-before relationship for data, so both the compiler (even on x86!) and CPU may reorder data = 42 after flag.store(true). Conflicting non-atomic access on data is full Undefined Behavior.

### Trap 4: [atomics.fences] §33.5.11 (cppcon-stage)

How can two threads safely synchronize non-atomic payload publication using relaxed atomic loads/stores plus fences?

```cpp
// T1: payload = 42;  /* A */;  flag.store(true, std::memory_order_relaxed);
// T2: if (flag.load(std::memory_order_relaxed)) { /* B */; use(payload); }
```

A. Put atomic_thread_fence(acquire) at A and release at B
B. Put atomic_thread_fence(release) at A and acquire at B
C. Fences cannot synchronize with relaxed atomic operations
D. Replace payload with volatile int

**Standard Answer (B):** Put atomic_thread_fence(release) at A and acquire at B

**Why:** A release fence followed by a relaxed store X synchronizes-with a relaxed load reading X followed by an acquire fence ([atomics.fences]). This pattern shines when looping over many items and fencing only once on success.

### Trap 5: [atomics.order] §33.5.4 (subtle)

When using std::atomic<int> x{0}; solely as a shared telemetry counter incremented from 8 threads (x.fetch_add(1, order)), which memory order is sufficient for accurate final totals?

A. memory_order_seq_cst is required so increments don't get lost
B. memory_order_acq_rel is required for read-modify-write atomicity
C. memory_order_relaxed is sufficient because all operations on a single atomic share one total modification order
D. Non-atomic volatile int is faster and equally safe

**Standard Answer (C):** memory_order_relaxed is sufficient because all operations on a single atomic share one total modification order

**Why:** Atomicity (indivisible read-modify-write so increments never lose updates) holds for EVERY memory_order including relaxed. You only need acquire/release/seq_cst when ordering OTHER memory operations relative to the counter.

---
Attribution: content from https://en.cppreference.com/w/cpp/language/memory_model (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.