(no title)
t43562 | 11 days ago
#define RETURN(x) result=x;goto CLEANUP
void myfunc() {
int result=0;
if (commserror()) {
RETURN(0);
}
.....
/* On success */
RETURN(1);
CLEANUP:
if (myStruct) { free(myStruct); }
...
return result
}
The advantage being that you never have to remember which things are to be freed at which particular error state. The style also avoids lots of nesting because it returns early. It's not as nice as having defer but it does help in larger functions.
vasama|11 days ago
You also don't have to remember this when using defer. That's the point of defer - fire and forget.
vbezhenar|11 days ago
t43562|11 days ago
There are several other issues I haven't shown like what happens if you need to free something only when the return code is "FALSE" indicating that something failed.
This is not as nice as defer but up till now it was a comparatively nice way to deal with those functions which were really large and complicated and had many exit points.
jagged-chisel|11 days ago
baq|11 days ago
Joker_vD|11 days ago
pocksuppet|11 days ago
t43562|11 days ago
bytejanitor|11 days ago
spiffyk|11 days ago