0

I'm compiling this code with gcc 11.2.0 and the -Wuninitialized switch, but I don't get any warning in spite of bar_uninitialized being used uninitialized:

#include <vector>
#include <iostream>

using std::vector;
using std::cout;

void foo(int dist, int tank, vector<int>& v)
{
  size_t bar_uninitialized;
  while (bar_uninitialized <= v.size())
  {
    bar_uninitialized++;
    cout << bar_uninitialized << "\n";
  }
}

int main()
{
  vector<int> v;
  foo(3, 3, v);
}

Is this a gcc compiler bug?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 4
    [Enable optimizations](https://www.godbolt.org/z/b81dqs3zc). GCC does a much better job at detecting uninitialized variables when it is optimizing, since then it does a much more thorough analysis of the code flow and variable accesses. Once upon a time, it didn't even try to produce uninitialized warnings unless optimizations were on. Now it at least tries a little, but it's still not nearly as good without optimizations. See also https://stackoverflow.com/questions/51659180/gcc-no-warning-about-an-uninitialized-array-with-o0/51660496#51660496. – Nate Eldredge Jan 28 '22 at 08:53
  • @NateEldredge Effectively, with `O3`, I get the warning, even without commenting the `insert`. – Damien Jan 28 '22 at 08:55
  • Not relevant here, but if you plan to use `v` later on, the while condition should probably read `bar_uninitialized < v.size()` so you don't try to access one past the end of `v`. – Eike Jan 28 '22 at 09:13
  • @Eike thanks, but yes it's not relevent here, the code I posted is just for demonstration, it has no other purpose,. – Jabberwocky Jan 28 '22 at 09:21

0 Answers0