UNINIT.CTOR.MUST

Uninitialized variable in constructor

The UNINIT.CTOR.MUST checker finds class variables that haven't been initialized in the constructor.

Vulnerability and risk

In C++, primitive data type variables need to be initialized explicitly. Use of uninitialized members in class methods typically leads to unpredictable behavior, and may have security implications.

Mitigation and prevention

To avoid use of uninitialized variables, make sure that constructors initialize all class fields.

Vulnerable code example

Copy
  class C {
      int i;
      int j;
    public:
      C() {
      this->j = 0; 
      }
  };

Klocwork flags line 6, indicating that the value of 'this->i' variable will remain uninitialized when the constructor exits.

Related checkers