Will the second branch of condition be executed in some case?
- Sì, potrebbe essere possibile, dipende da che cosa sta accadendo nel codice e quello che il compilatore sceglie di fare con il codice.
Shouldn't compiler warn about unreachable code?
- No, non può perché non c'è garanzia che è irraggiungibile
Prendete questo esempio:
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;
}
E l'output:
[email protected]:~> ./a.out
x is greater than 10!
x is less than 5! <-- Look, we hit the "unreachable code"
No, non verrà eseguito. La maggior parte dei messaggi identificano il codice irraggiungibile. –
Un linguaggio simile potrebbe sovraccaricare '>' e farlo fare la cosa inaspettata. –
Un'altra situazione a cui posso pensare dove viene eseguito il secondo ramo è se 'x' è una variabile globale e hai un altro thread che potrebbe cambiare il valore di' x' che inizialmente era maggiore di 10 a un valore inferiore a 5 dopo il primo test in una sfortunata circostanza. per esempio. 'x' è 11 all'inizio. Thread1 esegue il primo test 'if (x <10)' che è falso e successivamente, Thread2 cambia il valore di 'x' a 4. – halex