Question about GCC Optimizer and why this code always returns 42?
int main(int argc, char** argv) {
switch (argc) {
case 1000: return 42;
int y = 24;
default: return y;
}
return argc;
}
To explain this a bit more, a switch doesn't exactly do a linear progression. The logic equivalent to this would be:
"If argc is 1000, return 42. Otherwise return y"
The int y = 24;
is never used since it's never reached, the compiler can optimize this out, and since there's UB in the case of a default, it might as well return 42.
To fix this and behave the way I suspect you intend, you just need to declare y
outside of the switch statement.
int main(int argc, char** argv) {
int y = 24;
switch (argc) {
case 1000: return 42;
default: return y;
}
return argc;
}
Cases in a switch
are to be regarded as labels. If we translate your code to the equivalent goto-spaghetti, it might be easier to understand:
int main(int argc, char** argv)
{
if(argc == 1000)
goto label_1000;
else
goto label_default;
label_1000: return 42;
int y = 24;
label_default: return y;
return argc;
}
The goto label_default
jumps past the label initialization of y
and so it doesn't necessarily get executed. The same thing happens in your switch.
Best practice when declaring variables inside switches is therefore to always use a compound statement per case:
case 1000:
{
int y = 24;
break;
}
Apart from preventing spaghetti bugs, this also reduces the scope of the variable to the specific case
.