Search code examples
c++klocwork

c++ klocwork scan std::vector uninitialized


 > -Severity (Error)
 > -ID(local): 7
 > -Code: UNINIT.CTOR.MUST --> IpuCCAInterfaceBase()
 > -Message: 'this->_remaining_run_kernels_uuids' is not initialized
 > in this constructor.
 > -Details:
 > 
 > 'this->_remaining_run_kernels_uuids' is not initialized in this
 > constructor.
 > 
 > * IpuCCAInterfaceBase.cpp:47: passing 'this' to 'InitializeSaResults'
 > does not initialize 'this->_remaining_run_kernels_uuids'
 > 
 > * IpuCCAInterfaceBase.cpp:47: passing 'this' to 'CreateNVMData'
 > does not initialize 'this->_remaining_run_kernels_uuids'
 > 
 > * IpuCCAInterfaceBase.cpp:143: 'this->_remaining_run_kernels_uuids'
 > is used, but is uninitialized.
 > 
 > Current status 'Analyze'

This is the error I am getting

class COMMON_CCA_INTERFACE IpuCCAInterfaceBase : public ICCAOperations
{
private:

    vector<int> _remaining_run_kernels_uuids;
}

here is the header file

the cpp ctor

IpuCCAInterfaceBase::IpuCCAInterfaceBase()
{
   
}

we are using klocworks 20.1 what is the recommended way to solve this error?

the vector will be initialized and filled later in a different function. but when loading the class there is no data in it.


Solution

  • Perhaps Klockwork requires you to explicitly initialize the vector, for example in a constructor initializer list:

    IpuCCAInterfaceBase::IpuCCAInterfaceBase()
        : _remaining_run_kernels_uuids{ }
    {
    }
    

    With that said, I would consider the message a false positive, as the vector should be implicitly default-constructed (and thus initialized) even without this.