Why doesn't GCC report an uninitialized variable?

#include <ios>
#include <iostream>
#include <map>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  map<int, int> v;
  int i;
  int t;
  while (cin >> i) {
    v[i] = t++;
  }
  auto mi = i;
  auto mt = t;
  for (const auto p : v) {
    if (p.second < mt) {
      mi = p.first;
      mt = p.second;
    }
  }
  cout << mi << '\n';
  return 0;
}

The above program makes heavy use of an uninitialized variable t, but GCC does not report this using -Wall or -Wuninitialized. Why is this so?

It is worth noting that Klang catches him:

main.cpp:13:12: warning: variable 't' is uninitialized when used here [-Wuninitialized]
    v[i] = t++;
           ^

Used by g ++ (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2).

Clang version 4.0.1 is used (tags / RELEASE_401 / final).


As you can see at https://godbolt.org/g/kmYMC1 GCC 7.2 does not report this even if necessary. I will create a ticket in the GCC problem controller.

+4
source share
2 answers

GCC , , .

-O -Wall, :

<source>: In function 'int main()':
12 : <source>:12:13: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
     v[i] = t++;
            ~^~
Compiler exited with result code 0

https://godbolt.org/g/327bsi

+2

g++ -Wuninitialized: -Wmaybe-uninitialized.

, , g++ .

-Wmaybe-initalized : https://godbolt.org/g/3CZ6kT

, -Wmaybe-initalized -Wall, -Wextra.

+7

Source: https://habr.com/ru/post/1689309/


All Articles