I need ideas to try to make this program more readable. I think that it’s more a question about designer than other.
I have two loops:
for( ... ) {
//...
for(...) {
if(baa) {
goto outer;
}
//statements of 2-loop
}
//statements of 1-loop
}
it works fine, if baa has non-zero value, jump to outer label without run stataments of loop 1 and 2. But before return, I need to do some free() calls independent if goto outer was called:
if(a != NULL) free(a);
if(b != NULL) free(b);
but if I’m here because goto, the above code isn’t called.
And I need do the following:
goto outer; //if normally exited from two-loops, go to outer anyway.
outer: {
if(a != NULL) free(a);
if(b != NULL) free(b);
return ret;
}
What makes the function something like this:
char* foo(void) {
char *ret = NULL;
for( ... ) {
//...
for(...) {
if(baa) {
ret = tmp_result;
goto outer;
}
//some statements of 2-loop
}
//some statements of 1-loop
}
goto outer;
outer: {
if(a != NULL) free(a);
if(b != NULL) free(b);
return ret;
}
}
I hope this is clear. Thanks in advance.
The
outerlabel is ignored by the normal control flow, it’s only used as the target of thegoto, so you don’t need it after the loop. Further, there is no need to group the statements after the label into a compound statement,does exactly the same (and is IMO a bit nicer on the eyes).