Member not initialized in constructor
Constructor does not initialize some members of a class
Description
This defect occurs when a class constructor has at least one execution path on which it does not initialize some data members of the class.
The defect does not appear in the following cases:
Empty constructors.
The non-initialized member is not used in the code.
Risk
The members that the constructor does not initialize can have unintended values when you read them later.
Initializing all members in the constructor makes it easier to use your class. If you call a separate method to initialize your members and then read them, you can avoid uninitialized values. However, someone else using your class can read a class member before calling your initialization method. Because a constructor is called when you create an object of the class, if you initialize all members in the constructor, they cannot have uninitialized values later on.
Fix
The best practice is to initialize all members in your constructor, preferably in an initialization list.
Examples
Result Information
Group: Object oriented |
Language: C++ |
Default: Off |
Command-Line Syntax: NON_INIT_MEMBER |
Impact: Medium |
Version History
Introduced in R2015b
See Also
Find defects (-checkers)
| Copy constructor not called in initialization list
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)