Suppose these code compiled in g++
:
#include <stdlib.h>
int main() {
int a =0;
goto exit;
int *b = NULL;
exit:
return 0;
}
g++
will throw errors:
goto_test.c:10:1: error: jump to label ‘exit’ [-fpermissive]
goto_test.c:6:10: error: from here [-fpermissive]
goto_test.c:8:10: error: crosses initialization of ‘int* b’
It seems like that the goto
can not cross pointer definition, but gcc
compiles them ok, nothing complained.
After fixed the error, we must declare all the pointers before any of the goto
statement, that is to say you must declare these pointers even though you do not need them at the present (and violation with some principles).
What the origin design consideration that g++
forbidden the useful tail-goto statement?
Update:
goto
can cross variable (any type of variable, not limited to pointer) declaration, but except those that got a initialize value. If we remove the NULL
assignment above, g++
keep silent now. So if you want to declare variables that between goto
-cross-area, do not initialize them (and still violate some principles).