9

Consider the following code:

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> v{{1, 2, 3}};
    int a;

    std::cout << a << std::endl;    // 1

    for (const int x : v) {
        a = std::max(a, x);         // 2
    }

    std::cout << a << std::endl;

    return 0;
}

As modern compilers grew and now keep a watchful eye over dumb programmer mistakes, they track unitialized variables. This C++ code however, confuses them. So far, I get following results:

                        (1)      (2)
g++ 5.3.1
clang++ 3.7              ✔
Solaris Studio 12.5      ✔

As you can see, CLang and solstudio can detect only case (1) and ignore case (2), while g++ is ignoring both. Is there a complication to detect it in case (2)? Why g++ is so bad at this?

Compiler options I used:

$ g++-5 -std=c++11 -Wall -Wpedantic -pedantic -Wextra \
         -Wuninitialized -Wmaybe-uninitialized aisa.cpp
$ clang++ -std=c++11 -Wall -Wpedantic -pedantic -Wextra -Wuninitialized aisa.cpp
$ CC -std=c++11 -xprevise aisa.cpp
4
  • Consider filing a feature request on both gcc and clang's bug trackers - trivial situations like this one could probably be detected with additional logic Feb 8, 2017 at 16:09
  • 2
    What happens if you remove the first example? Do clang or Solaris Studio warn about the unitialized variable then? Feb 8, 2017 at 16:19
  • You may also get better warnings if you crank up the optimization level. Feb 8, 2017 at 16:19
  • 1
    @MartinBonner: g++-5 detects (1) with -O2, and removing case (1) doesn't help to make (2) catchable (actually I added case (1) after I've noticed that compiler failed to detect (2))
    – myaut
    Feb 8, 2017 at 16:27

2 Answers 2

12

std::max takes its arguments by const &, while the streaming operator << for ints takes the int by value. Passing an uninitialised object by reference is legal: for example, if the function just takes its address, all is well. Therefore, warning on passing a to std::max could easily be a false positive.

6
  • 1
    It indeed would make sense not to warn if it concerns a reference. But this is a const reference. I'd think this would be a situation in which the compiler actually could warn, as there is no way you could initialize an object passed over by a const reference.
    – Mar Tijn
    Feb 8, 2017 at 16:14
  • 1
    @MarTijn What if the sole purpose of the function was to take a const * to the object and use it as a map key? You can do this from a const &, and you can do this even from an uninitialised object. Feb 8, 2017 at 16:18
  • @MarTijn: If the function saves the address of the object, then something later can initialize the variable. (Or the function can use the address alone as some sort of unique tag). Feb 8, 2017 at 16:20
  • Valid points! TBH I never use constructs like this, so a compiler option would perhaps be preferable.
    – Mar Tijn
    Feb 8, 2017 at 16:24
  • You point seems valid, but int mymax(int a, int b) { return (a>b)?a:b; } doesn't help much, case (2) still not covered
    – myaut
    Feb 9, 2017 at 9:57
3

First of all: Both compilers only diagnose the first offense, that is they only report the first uninitialized use of a. So to get a warning for the second one, we need to remove that first line:

#include <vector>
#include <algorithm>
#include <iostream>

int main() {
    std::vector<int> v{{1, 2, 3}};
    int a;

    for (const int x : v) {
        a = std::max(a, x);         // 2
    }

    std::cout << a << std::endl;

    return 0;

}

Now we see two unrelated compiler quirks: clang does not include -Wconditional-uninitialized in -Wall and -Wextra. If you enable that, you do get a warning at the std::cout because it potentially prints an uninitialized variable.

gcc on the other hand only tracks uninitialized variables when the optimizer is enabled, probably to speed up compilation of debug builds. With -O2 -Wall, gcc 6 throws a warning in both cases, though without pinpointing the location as exactly as clang does in the second case. (gcc <= 5.3 does not warn about the second case as you observed, so it seems that was implemented recently.)

So TL;DR: You did not invoke your compilers right.

3
  • "First offense" meaning the first occurrence of a specific warning, right? Not a warning in general?
    – anon
    Mar 3, 2017 at 22:05
  • @QPaysTaxes Yes. Feel free to edit if you can state that more clearly.
    – Baum mit Augen
    Mar 3, 2017 at 22:06
  • Yeah, I've used g++ 5. Thanks for your answer!
    – myaut
    Mar 10, 2017 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.