| ▲ | jonhohle 3 hours ago | |||||||
It’s pedantic, but in the malloc example, I’d put the defer immediately after the assignment. This makes it very obvious that the defer/free goes along with the allocation. It would run regardless of if malloc succeeded or failed, but calling free on a NULL pointer is safe (defined to no-op in the C-spec). | ||||||||
| ▲ | flakes an hour ago | parent | next [-] | |||||||
I'd say a lot of users are going to borrow patterns from Go, where you'd typically check the error first.
IMO this pattern makes more sense, as calling exit behavior in most cases won't make sense unless you have acquired the resource in the first place.free may accept a NULL pointer, but it also doesn't need to be called with one either. | ||||||||
| ||||||||
| ▲ | masklinn an hour ago | parent | prev | next [-] | |||||||
It seems less pedantic and more unnecessarily dangerous due to its non uniformity: in the general case the resource won’t exist on error, and breaking the pattern for malloc adds inconsistency without any actual value gain. | ||||||||
| ▲ | 2 hours ago | parent | prev [-] | |||||||
| [deleted] | ||||||||