liveBook · Manning

Secondary block

As explained in Modern C, Third Edition

A secondary block is the block formed by the loop body associated with a for construct in the terminology used here.

A secondary block is the loop body associated with a for construct. The for construct forms the primary block; its body forms the nested secondary block, usually delimited by braces. section 2.2

The distinction matters when block structure controls local-object lifetime:

  • Treat each loop iteration as executing the secondary block and defining a new instance of a local object. section 19.3
  • Do not access an object after its secondary block ends; a pointer to a compound literal created there becomes invalid. section 19.3
  • Prefer the loop form when early release and resource reuse matter; a goto-based repeated block can keep the object alive longer. section 19.3
 size_t* ip = nullptr;
 while(something)
   ip = &(size_t){ fun() };     /* Life ends with while       */
                              /* Good: resource is released */
 printf("i is %d", *ip)       /* Bad: object is dead        */

A compound literal created inside the loop body ends its lifetime with that secondary block, making the later dereference invalid. section 19.3

How Modern C, Third Edition covers it

How deep it goes
Explains block nesting and iteration-based object lifetime, stopping short of a broader formal treatment of control-flow semantics.
Examples
Uses loop and goto listings that assign pointers to compound literals and contrast their lifetimes after repetition.
What it assumes
Assumes familiarity with C declarations, functions, braces, loops, pointers, and compound literals while defining block terminology explicitly.
What you can do after
Evaluate whether pointers remain valid when local objects are created inside loop bodies or repeated goto blocks.
How it connects
Connects secondary blocks with block scope, primary blocks, for constructs, object lifetime, compound literals, and goto.

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.