3

This code:

class X {
};

void f(const void *) {
}

int main() {
    X a;
    void *p = &a;
    f(p);
}

compiled with g++ -Wall test.cpp issues warning

test.cpp: In function ‘int main()’:
test.cpp:10:6: warning: ‘p’ may be used uninitialized [-Wmaybe-uninitialized]
   10 |     f(p);
      |     ~^~~
test.cpp:4:6: note: by argument 1 of type ‘const void*’ to ‘void f(const void*)’ declared here
    4 | void f(const void *) {
      |      ^
test.cpp:8:7: note: ‘a’ declared here
    8 |     X a;
      |       ^

However when optimizations are enabled (with -O1, -O2, or -O3) the warning disappears.

Is this a compiler bug?

vbstb
  • 1,261
  • 1
  • 10
  • 14
  • When optimization is turned on, all your empty classes and functions without statements are optimized out. Thus, no warnings. – DYZ Aug 14 '21 at 02:48
  • Which version of GCC are you using? It doesn't happen with [GCC 4.1.2](https://godbolt.org/z/6Eq68oj3f). (If you want to see the version, just run `g++ -v` in the command line.) – Ruks Aug 14 '21 at 02:50
  • @Ruks gcc 11.1. Seems that it doesn't happen with 10.3 either. – vbstb Aug 14 '21 at 02:54
  • @vbstb It seems [there are already a lot of bug reports on false "*-Wmaybe-uninitialized*" warnings](https://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=maybe%20uninitialized) just like yours. You *could* try filing up a bug report there. – Ruks Aug 14 '21 at 02:59
  • @Ruks Got it. Thanks. – vbstb Aug 14 '21 at 03:00

0 Answers0