
Originally Posted by
rohcQaH
probably because a lot of the resources needing cleanup are bound to local variables, which wouldn't be available in a separate cleanup() function.
Using macros you can have clean looking code without goto's and without the overhead of functions.
Code:
int myfunc(...args...)
{
...local vars myvar1, myvar2...
#define getout() \
do \
{ \
free(myvar1); \
free(myvar2); \
return failure; \
} while(0)
...allocate myvar1, myvar2...
do_something();
if (failure) { getout(); }
do_morestuff();
if (failure) { getout() }
return success;
}
Of course, what getout() does is specific to the state of the function e.g. myvar1 may not even allocated.
If you are OCD'ed to 'goto', you can call the macro goto_out...;-)