While writing some code for a code golf competition, I noticed some strange behavior. For example:
int main(void)
{
goto jmp;
char *str = "Hello, World!";
jmp:
puts(str);
}
Compiling with GCC (and Clang and MSVC) results in no warnings or errors, but running it throws SIGSEGV
. How come the compilers don't catch that the goto
is jumping around the variable declaration?
I decided to test this (bug?) out, and rewrote the example:
int main(void)
{
goto jmp;
int x;
jmp:
putchar(x);
}
Again, compilation yields no errors. Furthermore, nothing is thrown upon execution, yet in MSVC the process exits with a non-zero exit code.
What's going on here? Is this simply another reason we shouldn't use goto
s? And how come no errors are thrown in the second example, while SIGSEGV
is thrown in the first?