COP-member-uninit (C++ only)
Synopsis
A member of a class is not initialized in one of the class constructors.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
A member of a class is not initialized in one of the class constructors. This might cause unexpected or unpredictable program behavior, and can be very difficult to identify as the cause. Because members of built-in types are not given a default initialization, constructors must initialize all members of a class. Even if this is intentional (and the attribute is initialized elsewhere) it is still dangerous, because it subverts the Resource Acquisition is Initialization convention, and consequently users of the class might not initialize the attribute. Uninitialized data can lead to incorrect program flow, and might cause the application to crash if the class handles dynamic memory.
Coding standards
- CWE 456
Missing Initialization
Code examples
The following code example fails the check and will give a warning:
struct S{
int x;
S() {} //this constructor should initialize x
};
The following code example passes the check and will not give a warning about this issue:
struct S{
int x;
S() : x(1) {} //OK - x is initialized
};