liveBook · Manning

State

As explained in Modern C, Third Edition

Hidden state is implementation or library state that affects program behavior without being directly represented by ordinary program values.

A `mtx_t` object has a lock state that affects whether a thread can proceed, even though that state is not an ordinary value the program reads. A `once_flag` has initialization state, and an `atomic_flag` can be set or cleared. These are forms of hidden state: parts of an execution’s abstract state alongside points of execution, intermediate values, and stored values. A change to hidden state is an effect. section 21.0

Ordinary stored values matter too. If a function receives pointers, a call may change an object through either pointer; when the pointers alias, an assignment through one can change a later read through the other. Reasoning about state therefore requires asking what can change, not just inspecting the local expression. The full abstract state may be difficult to determine and, with concurrent threads, may not have a single fine-grained moment at which to inspect it. section 12.3 section 21.0

#pragma FP_CONTRACT OFF
#pragma FENV_ROUND FE_TONEAREST

Floating-point settings are a less visible dependency. These pragmas select contraction and rounding behavior; a calculation that appears to depend only on its arguments may also depend on the floating-point environment. section 16.3

TAKEAWAY: Check implicit state before treating a function as pure. A function marked `[[unsequenced]]` must not read nonconstant global variables or system state, or visibly modify them. Floating-point arithmetic generally prevents that designation because its behavior can depend on state such as the rounding mode. section 16.3

How Modern C, Third Edition covers it

How deep it goes
Defines hidden state within the abstract state machine and works through aliasing and floating-point dependencies, without specifying lock or atomic internals.
Examples
Uses mutex lock state, one-time initialization, atomic flags, pointer aliasing, and floating-point pragmas as concrete cases.
What it assumes
Assumes familiarity with C expressions and functions; builds the abstract-state vocabulary from values, objects, and execution points.
What you can do after
Evaluate/understand whether a function’s apparent inputs capture its state dependencies before assigning `[[unsequenced]]`.
How it connects
Connects hidden state to the abstract state machine, pointer aliasing, floating-point environment, pure functions, and concurrency.

Also explained in

Compare how 39 books explain state →

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.