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.

Hidden state affects execution without appearing as an ordinary argument or stored value. For example, floating-point rounding mode is execution state that can affect a function’s result even when its explicit arguments are unchanged. section 16.3

#pragma FP_CONTRACT OFF
#pragma FENV_ROUND FE_TONEAREST

These pragmas disable floating-point expression contraction and select rounding to the nearest representable number. A function using floating-point arithmetic can depend on such thread-local state. section 16.3

For an [[unsequenced]] function, hidden dependencies matter: it must not read nonconstant global variables or system state, or visibly modify them. In general, a function using floating-point arithmetic is not pure and should not have this attribute. section 16.3

Hidden state is part of a broader execution state. The abstract state also includes points of execution, intermediate values, and stored values. Effects can change hidden state, as with the lock state of an mtx_t object, the initialization state of a once_flag, or operations on an atomic_flag. section 21.0

 double blub(double const* a, double* b) {
   double myA = *a;
   *b = 2*myA;
   return *a;      // May be myA or 2*myA
 }

Even stored values can be difficult to track. If a and b point to the same object, writing through b changes what is read through a, despite a pointing to const. section 12.3

In multithreaded execution, the entire abstract state is generally not observable, and an overall fine-grained notion of time across threads is not assumed. Reason about relevant effects and synchronization rather than relying on a single global snapshot. section 21.0

How Modern C, Third Edition covers it

How deep it goes
Defines execution state and effects, then explains floating-point state dependencies and pointer aliasing.
Examples
Uses rounding-mode pragmas, aliased pointer arguments, and mutex, once-flag, and atomic-flag states.
What you can do after
Evaluate function purity and execution effects by identifying dependencies on implicit state.
How it connects
Connects execution state to the abstract state machine, pointer aliasing, function purity, and synchronization.

Also explained in

Compare how 41 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.