Why do I not get compiler warning about access uninitialized member variable in ctor?

c++, compiler-warnings, constructor, initialization

Solution

Short Answer

As pointed out in the comments, reading from an uninitialized variable is undefined behavior. Compilers are not obligated by the standard to provide a warning for this.

(In fact, as soon as your program expresses undefined behavior, the compiler is effectively released from any and all obligations...)

From section [defns.undefined] of the standard (emphasis added):

undefined behavior

behavior for which this International Standard imposes no requirements

[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. —end note ]

Long Answer

This can be a difficult situation for a compiler to detect (and if it does detect it, it's difficult to inform the user about it in some useful way).

Your code only exhibits undefined behavior because it's trying to read from uninitialized member variables `width` and `height`. The fact that they're member variables is one of the things that can make this situation tricky to diagnose.

With local variables, the static analysis involved in detecting this can be relatively straightforward (not all the time, mind you).

For example, it's very easy to see the problem here:

    int foo()
    {
        int a;
        int b = 0;

        return a + b; // Danger! `a` hasn't been initialized!
    }

How about in this scenario:

    int foo(int& a)
    {
        int b = 1;

        return a + b; // Hmm... I sure hope whoever gave me `a` remembered to initialize it first 
    }

    void bar()
    {
        int value;
        int result = foo(value); // Hmm... not sure if it matters that value hasn't been initialized yet
    }

As soon as we start dealing with variables whose scope extends beyond a single block, it's very difficult to detect whether or not a variable has been initialized.

Now, relating this back to the problem at hand (your question): the variables `width` and `height` are not local to the constructor - they could have been initialized outside the constructor.

For example:

    Image::Image(int _width, int _height)
    {
      Initialize();

      array = new int[width * height]; // Maybe these were initialized in `Initialize`...

      width = _width;
      height = _height;
    }

    Image::Initialize()
    {
        width = 0;
        height = 0;
    }

Should the compiler emit a warning in this scenario?

After some cursory analysis we can conclusively say "no, it shouldn't warn", because we can see that the `Initialize` method does indeed initialize the member variables in question.

But what if `Initialize` delegates this to another method `MoreInitialize()`? And that method delegates it to another method `YetEvenMoreInitialize`? This begins to look like a problem that we can't reasonably expect the compiler to solve.

Problem

Here is a simple test case that compiles without any warning. Looks like a common mistake but clang, gcc and visual studio doesn't emit warning in this case. Why? ``` class Image { private: int width, height; int* array; public: Image(int _width, int _height); void crashTest(); }; Image::Image(int _width, int _height) { array = new int[width * height]; // ^^^^^ ^^^^^^ this is wrong // I expect a warning here e.g.: 'width is uninitialized here' width = _width; height = _height; } void Image::crashTest() { for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) array[x + y * width] = 0; } } int main() { const int ARRAY_SIZE = 1000; Image image(ARRAY_SIZE, ARRAY_SIZE); image.crashTest(); return 0; } ``` e.g.: ``` g++ -Wall -Wextra -O2 -Wuninitialized test.cpp clang++ -Wall -Wextra -O2 -Wuninitialized test.cpp ``` gives me no warning

Original source