5

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Tom Cornebize
  • 1,362
  • 15
  • 33
  • does it for me: `warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized] for(int i ; i < 10 ; i++) {` Compile with `-Wall -Wextra`. – vsoftco Jun 01 '15 at 16:33
  • 1
    [Does for me, with just `-Wall`](http://coliru.stacked-crooked.com/a/58bc57b1ce9b4ed0); state your compiler version as there have been many, many, many versions of GCC. Or simply upgrade. – Lightness Races in Orbit Jun 01 '15 at 16:33
  • This may (or may not?) be a helpful post: http://stackoverflow.com/questions/28551951/is-it-legal-to-increment-non-initialized-variable (Even though it's pretty downvoted, there are some good answers there.) – Serlite Jun 01 '15 at 16:34
  • My version: gcc version 4.8.2 (Debian 4.8.2-1) – Tom Cornebize Jun 01 '15 at 16:36
  • The `Wextra` flag does not change anything for me. – Tom Cornebize Jun 01 '15 at 16:37
  • Related https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings. – Pradhan Jun 01 '15 at 17:03

1 Answers1

3

Thanks to Pradhan who gave this link, I understood the problem.

This link states the following:

GCC has the ability to warn the user about using the value of a uninitialized variable. Such value is undefined and it is never useful. It is not even useful as a random value, since it rarely is a random value. Unfortunately, detecting when the use of an uninitialized variable is equivalent, in the general case, to solving the halting problem. GCC tries to detect some instances by using the information gathered by optimisers and warns about them when the option -Wuninitialized is given in the command line. There are a number of perceived shortcomings in current implementation. First, it only works when optimisation is enabled through -O1, -O2 or -O3. Second, the set of false positives or negatives varies according to the optimisations enabled. This also causes high variability of the warnings reported when optimisations are added or modified between releases.

Indeed, when I add one of these flags, the compiler yields the warning.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Tom Cornebize
  • 1,362
  • 15
  • 33