liveBook · Manning

Consistency model

As explained in Modern C, Third Edition

The C memory model is the language abstraction that defines how objects, storage, pointers, and program state relate without requiring a particular physical-memory arrangement.

The C memory model describes program state through objects, storage, values, representations, and pointers rather than fixed physical addresses. Applying & to an object produces an address that can be used through a pointer to inspect or modify execution state. That address may correspond to virtual memory, a file, or an I/O port; portable C does not require a particular physical arrangement. section 12.0 section 12.3

Figure 12.1 The different levels of the value-memory model for an int32_t. Example of a platform that maps this type to a 32-bit signed int and uses a little-endian object representation.
figure

A layered diagram traces an int32_t from value and object representation through binary representation, including a 32-bit signed mapping and little-endian layout. section 12.0

The model separates several related concerns:

  • Physical memory is hidden behind virtual memory, giving processes a portable address space and protecting memory they do not own. section 12.0
  • Each object consists of bytes called its object representation; unions can overlay different types on that representation. section 12.0 section 12.8
  • Pointer types are distinct when their base types differ, and, except for character types, only pointers with the same base type may alias. section 12.0 section 12.3
  • Storage may begin as uninterpreted bytes and acquire a type when a value is stored; storage duration, lifetime, visibility, initialization, and alignment then constrain access. section 13.0 section 12.8

Aliasing changes what a function may return

Given `double blub(double const* a, double* b)`, calling the function with distinct addresses leaves `*a` unchanged, but calling `blub(&c, &c)` lets the assignment through `b` change what `a` observes. The function may therefore return either the original value or the modified value. With `size_t const* a` and `double* b`, the different base types rule out that aliasing case, so the return must be the original value. section 12.3

How Modern C, Third Edition covers it

How deep it goes
Descends from physical and virtual memory through storage, objects, representations, alignment, and aliasing, while deferring detailed lifetime rules.
Examples
Uses pointer-based functions, aliasing calls, unions, byte arrays, addresses, and an int32_t representation diagram.
What you can do after
Evaluate pointer accesses and object representations while recognizing portability, aliasing, type, alignment, and storage constraints.
How it connects
Connects pointers, virtual memory, object representation, unions, casts, effective types, alignment, aliasing, and object lifetime.

Also explained in

Compare how 4 books explain consistency model →

Related concepts

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