C++ enable warning for uninitialized variables in classes

Viewed 1695

Lets have this code:

#include <iostream>
#include <simd/simd.h>
class X {
public:
    X(int a) : x{a} {} // <-- only x is initialized, y is not

    int x;
    int y;
    simd_double3 d;
};

int main(int argc, const char * argv[]) {
    X x(1);
    X* xx = new X(2);
    std::cout<<x.x<<" "<<x.y<<" "<<x.d.x; // <-- y and x.d are used, but not initialized
    std::cout<<xx->x<<" "<<xx->y<<"END\n";
    return 0;
}

I want to emit warning that y in X is not initialized. -Wall, -Wmissing-field-initializers seems to do nothing. It compiles without warnings. This sample code produces this output: 1 0 6.95323e-310 So even if y is initialized to 0(which is not, because clang analysis marks it as uninitialized), clearly simd_double3 is not initialized to 0.

Also clang analysis mark x.y as uninitialized. (1st function call argument is an uninitialized value)

Also, when creating X on heap in release mode, content of x.y is garbage. Second line prints: 2 -1094795586, y is clearly not initialized.

1 Answers
Related