GOTO Considered Totally Sick (for error handling in C)
Web page · https://thestateofinahaze.net/blog/c-goto-considered-totally-sick
First seen by Oriel on .
AI summary
This post argues that C’s goto can provide cleaner, safer error handling by centralizing resource cleanup at a single exit point.
- Go’s defer and Zig’s errdefer simplify cleanup, while C requires explicitly planning cleanup for each possible failure.
- The rewritten some_func initializes pointers, jumps to free_mem on failure, and frees every allocated S before returning NULL.
- Centralized cleanup reduces duplicated error-handling code and makes later changes less likely to omit necessary resource releases.
- Multiple labels can divide cleanup into resource-specific sections, and feedback recommended calloc for allocations whose size multiplication might overflow.
- The pattern is also mentioned in the Linux kernel coding style guide.
#c #error handling #memory management #goto
Generated by gpt-5.6-sol. Check the original for details.
Metric trends
Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained
Points
Score reported by the source.
- Lobsters: 44, latest observation ; +42 over observed period since
Comments
Comment count reported by the source.
- Lobsters: 13, latest observation ; +13 over observed period since
Aliases
| Source | External ID | URL |
|---|---|---|
| Lobsters | exwlmn | https://thestateofinahaze.net/blog/c-goto-considered-totally-sick/ Read discussion |