ho riprodurre il problema da questo semplice demo:bug g ++? (Bool_val 0: 1) restituisce né 0 né 1
// bool_test_func.cpp
#include <stdio.h>
void func(bool* b) {
int a = (*b ? 0 : 1);
printf("%d\n", a); // EXPECT ether 0 or 1 here
}
// bool_test.cpp
void func(bool* b);
int main() {
int n = 128;
func((bool*)&n);
return 0;
}
-O0 compilare ed eseguire:
g++ -g -O0 -Wall -o bool_test bool_test.cpp bool_test_func.cpp
[email protected]:~/testing/c++$ ./bool_test
0
-O1 compilare ed eseguire (inaspettato risultato):
g++ -g -O1 -Wall -o bool_test bool_test.cpp bool_test_func.cpp
[email protected]:~/testing/c++$ ./bool_test
129
Quando controllo il codice -O2 ASM, ritengo sia un errore g ++, g ++ 's optimzation codice pensare sempre il valore booleano è un etere 1 o 0:
00000000004005e6 : 4005e6: 48 83 ec 08 sub $0x8,%rsp 4005ea: 0f b6 37 movzbl (%rdi),%esi 4005ed: 83 f6 01 xor $0x1,%esi #just XOR the bool val 4005f0: 40 0f b6 f6 movzbl %sil,%esi 4005f4: bf 94 06 40 00 mov $0x400694,%edi 4005f9: b8 00 00 00 00 mov $0x0,%eax 4005fe: e8 9d fe ff ff callq 4004a0 400603: 48 83 c4 08 add $0x8,%rsp 400607: c3 retq 400608: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1) 40060f: 00
gcc version 4.9.2 (Debian 4.9.2-10)
È questo comportamento g ++ in base alla progettazione? Come posso disattivare questa ottimizzazione errata? Grazie ~
gcc versione 4.9.2 (Debian 4.9.2-10) – mikewei
Interessante '# include'. – erip
L'ottimizzazione non è sbagliata: un bool può contenere solo 0 o 1. Quello che stai passando è un tipo "int" punito come "bool", ne deriva un comportamento indefinito. – Quentin