liveBook · Manning

Local static state

As explained in Modern C, Third Edition

Local static state is state held by objects with static storage duration that are declared within a function or local program region.

Local static state is internal state held by objects with static storage duration inside a function or local program region. Its persistence matters when function attributes promise reproducible or unsequenced behavior. section 16.3

Apply these rules when annotating functions:

  • An [[unsequenced]] function shall not modify local static state, directly or through another function call. section 16.3
  • A [[reproducible]] function may modify local static state when that state is not observable outside the function. section 16.3

Caching inside hash

A hash function can memorize results in an internal static table, making repeated calls more efficient while preserving the same visible result. That hidden cache can support [[reproducible]], but the function cannot be [[unsequenced]] because calls must remain properly sequenced. section 16.3

size_t hash(char const[restrict static 32]) [[reproducible]];

Compare the reproducible hash declaration with the hidden-state rule. section 16.3

How Modern C, Third Edition covers it

How deep it goes
Defines local static state and distinguishes direct, indirect, observable, and unobservable modification through concrete attribute rules.
Examples
Uses a static hash table that caches results without changing the function's externally observable state.
What you can do after
Evaluate whether a function's persistent internal state permits [[unsequenced]] or [[reproducible]] annotation.
How it connects
Connects static storage duration with unsequenced functions, reproducible functions, sequencing, and memoization.

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.