1

In the following code:

#include <iostream>
class A {
public:
    void show_all(){std::cout << x1 << ", " << y1 << ", " << z1 << std::endl;}
    void show_uninit();
private:
    int x1;
    int y1;
    int z1;
};
void A::show_uninit()
{
    int i;
    int j;
    int k;
    std::cout << i << ", " << j << ", " << k << std::endl;
    return ;
}
int main()
{
    A a;
    a.show_all();
    a.show_uninit();
    return 0;
}

Which got the the following warning after compiled:

$ g++ -Wall test_construction.cpp
test_construction.cpp: In member function ‘void A::show_uninit()’:
test_construction.cpp:22:18: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
     std::cout << i << ", " << j << ", " << k << std::endl;
                  ^
test_construction.cpp:22:31: warning: ‘j’ is used uninitialized in this function [-Wuninitialized]
     std::cout << i << ", " << j << ", " << k << std::endl;
                               ^
test_construction.cpp:22:44: warning: ‘k’ is used uninitialized in this function [-Wuninitialized]
     std::cout << i << ", " << j << ", " << k << std::endl;
                                        ^

My understanding is the member a.x1, a.y1 and a.z1 should have got the same warnings that i, j and k got because object "a" was defined in function main() by stating "A a;", though it was declared in class out of any function. Thanks!

T.C.
  • 133,968
  • 17
  • 288
  • 421
Joe.Z
  • 2,725
  • 4
  • 25
  • 30
  • 3
    Because the compiler isn't smart enough to warn you about it. Rest assured, they *will* have unpredictable values like any other uninitialized variable. – user253751 Feb 15 '15 at 03:21
  • you are correct, the compiler should have warned you about it. since you didn't call a constructor, the members are not initialized, you could have also called the default constructor: A a{}; as we all know, if there is no one, the compiler would generate it for you, giving default values to the member –  Feb 15 '15 at 03:54
  • 2
    @blade A constructor is called (conceptually, anyway). It simply did nothing. – T.C. Feb 15 '15 at 05:02

1 Answers1

4

The compiler isn't smart enough to generate a warning in this case.

Note: The fact that the compiler doesn't produce any warnings or errors doesn't mean your code is right. A lot of people don't realise this when they start programming.

Compilers aren't even required to generate warnings for show_uninit, although many do, because it's relatively easy to detect a local variable that is never initialized. (If i, j and k were initialized sometimes, you might not get a warning)

When executed, show_all will print unpredictable values, just like show_uninit.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • g++ warns on `show_all()` as well with optimizations on, probably because of inlining. – T.C. Feb 15 '15 at 05:03
  • 1
    When executed, `show_all` causes undefined behaviour which means that [anything can happen](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). It need not print anything or even execute at all. – M.M Feb 15 '15 at 05:04