Interesting .. yet I can see a fairly simple way to restructure this C code, without either of the goto statements ..
something *
alloc_something(void)
{
/* Make two malloc requests. Insure both succeed; return
allocated memory, if any. Three possible logic paths:
1. First malloc fails, and we are done.
2. First malloc succeeds, second malloc fails: free
the first allocated block, and we are done.
3. First and second mallocs succeed, and we are done.
*/
something * ret = malloc(sizeof(something));
/* Did the first request succeed? */
if (ret != NULL) {
ret->another_thing = alloc_another_thing();
/* Did the second request fail? */
if (ret->another_thing == NULL) {
free(ret);
ret = NULL;
}
}
return ret;
}
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.