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
