Ответ 1
Will the second branch of condition be executed in some case?
- Да, это возможно, это зависит от того, что еще происходит в коде и от того, что компилятор хочет сделать с вашим кодом.
Shouldn't compiler warn about unreachable code?
- Нет, это невозможно, потому что нет гарантии, что он недоступен
Возьмите это, например:
int x = 11;
void* change_x(){
while(1)
x = 3;
}
int main(void)
{
pthread_t cxt;
int y = 0;
pthread_create(&cxt, NULL, change_x, NULL);
while(1){
if(x < 10)
printf("x is less than ten!\n");
else if (x < 5){
printf("x is less than 5!\n");
exit(1);
}
else if(y == 0){ // The check for y is only in here so we don't kill
// ourselves reading "x is greater than 10" while waiting
// for the race condition
printf("x is greater than 10!\n");
y = 1;
}
x = 11;
}
return 0;
}
И вывод:
[email protected]:~> ./a.out
x is greater than 10!
x is less than 5! <-- Look, we hit the "unreachable code"