goto transfers execution to a labeled statement within the same function. Labels mark destinations; the jump interrupts ordinary control flow and can repeat a surrounding block. section 17.3 section 13.3
A repeated block
size_t* ip = 0; RETRY: ip = &(size_t){ fun() }; if (condition) goto RETRY; printf("i is %d", *ip); The label makes RETRY a local destination. Unlike an equivalent loop, the compound literal's surrounding block keeps the object alive, so the final access is valid; the trade-off is that the resource remains occupied until the block ends. section 17.3
Use goto cautiously:
- Reserve it for exceptional control-flow changes, especially transitional errors requiring local cleanup. section 17.3
- Distinguish scope from lifetime: jumping around a definition can interact subtly with automatic objects and compound literals. section 13.3
- Recognize algorithmic uses as well: a recursive descent parser uses short jumps to restart output handling or ascend. section 17.1
1 size_t* ip = 0
2 while(something)
3 ip = &(size_t){ fun() }; /* Life ends with while */
4 /* Good: resource is freed */
5 printf("i is %d", *ip) /* Bad: object is dead */
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":19}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":9}],[{\"line\":2,\"ch\":56},{\"line\":2,\"ch\":61}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":13},{\"line\":2,\"ch\":19}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":10}]]"}
!@%STYLE%@!
Compare loop repetition with labeled repetition to inspect how object lifetime and later pointer access differ. section 17.3

