What is the recommended way to use defer to free values only on an error path (rather than all paths)? Currently I use goto for this: void* p1 = malloc(...); if (!p1) goto err1; void* p2 = malloc(...); if (!p2) goto err2; void* p3 = malloc(...); if (!p3) goto err3; return {p1, p2, p3}; err3: free(p2); err2: free(p1); err1: return NULL; With defer I think I would have to use a "success" boolean like this: bool success…
I initialise all pointers to NULL at the top of the function and use `goto cleanup`, which cleans up everything that is not being returned ... because `free(some_ptr)` where `some_ptr` is NULL is perfectly legal.