liveBook · Manning

Memory location

As explained in C++ Concurrency in Action, Second Edition

A memory location in C++ is a storage unit consisting of an object of scalar type or a sequence of adjacent bit-fields.

A C++ memory location is either an object of scalar type or a sequence of adjacent bit-fields. An object occupies at least one memory location; adjacent bit-fields share one, even though they are distinct objects. Fundamental-type variables each occupy exactly one memory location, regardless of size or adjacency. section 5.1.1

Bit-fields in a struct

In the illustrated struct, adjacent bit-fields bf1 and bf2 share a memory location. An unnamed zero-length bit-field separates bf4 into its own location but occupies none itself. The example shows why object boundaries and memory-location boundaries are not always the same. section 5.1.1

Concurrency safety depends on which locations threads access. Concurrent reads of the same location are safe, but if either thread writes, accesses need an enforced ordering. Without ordering, conflicting non-atomic accesses constitute a data race and cause undefined behavior. Mutexes or atomic operations can establish ordering. section 5.1.2

Figure 5.1. The division of a struct into objects and memory locations

Use the struct diagram to distinguish objects, subobjects, and the memory locations they occupy. section 5.1.1

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Defines locations through object and bit-field layout, then explains data-race conditions; defers atomic ordering details.
Examples
Shows a struct with adjacent bit-fields and a zero-length separator, plus shared-location thread accesses.
What you can do after
Evaluate whether concurrent accesses to a memory location require ordering to avoid a data race.
How it connects
Connects C++ objects, subobjects, bit-fields, data races, mutexes, and atomic operations.

Also explained in

Compare how 3 books explain memory location →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.