Cleanup code releases resources and restores required state when an operation completes or fails. In C, error paths must still free allocated memory and preserve required state such as errno. section 14.5
One cleanup path for exceptional exits
An optimized number-printing function allocates a buffer, enlarges it with realloc when necessary, and may encounter allocation failure or a result exceeding INT_MAX. Rather than duplicate free calls across nested conditions, it uses goto to jump to shared cleanup code. The normal path remains readable while exceptional exits converge on resource release. section 14.5
Keep the control-flow rules explicit:
- Place cleanup at a clearly marked label within the same function. section 14.5
- Do not jump over variable initializations. section 14.5
- Use the technique pragmatically: duplicated or deeply nested error handling can be harder to follow. section 14.5





