I tried to compile the following code using g++ (gcc version 4.8.2 (Debian 4.8.2-1)), with -Wall flag (adding the -Wextra flag does not change anything for me).
#include <iostream> using namespace std ; int main() { int i ; cout << i << endl ; } It gave this warning:
test.cpp: In function ‘int main()’: test.cpp:7:13: warning: ‘i’ is used uninitialized in this function [-Wuninitialized] cout << i << endl ; But the following code does not yield any warning:
#include <iostream> using namespace std ; int main() { for(int i ; i < 10 ; i++) { cout << i << endl ; } } I did further tests.
The following yields the warning:
#include <iostream> using namespace std ; int main() { int i ; while(i<10) { cout << i << endl ; } } But the following does not:
#include <iostream> using namespace std ; int main() { int i ; while(i<10) { cout << i << endl ; i++ ; } } In the above program, if I replace the while by an if, then I have a warning.
Is there some explanation to this? Why can the compiler recognize the problem in some cases and not in others, although they seem very close?
warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized] for(int i ; i < 10 ; i++) {Compile with-Wall -Wextra.-Wall; state your compiler version as there have been many, many, many versions of GCC. Or simply upgrade.Wextraflag does not change anything for me.