Thread: Coverity report "Uninitialized scalar variable"

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    17

    Coverity report "Uninitialized scalar variable"

    Hi , my code get "Uninitialized scalar variable" when Coverity analysis the code .code like below:

    Code:
    struct TEST_D
    {
        void* por;
        int nsize;
            void* pn;
        TEST_D* pNext;
    };
    
    typedef std::map<void*, TEST_D> DATAMAP;
    
    int testData(void* por,int nsize, void* pn = null)
    {
        TEST_D data;
        data.por= por;
        data.nsize= nsize;
            data.pn= pn;
    
            //Uninitialized scalar variable (UNINIT) uninit_use: Using uninitialized value data. Field data.pNext is uninitialized
        DATAMAP[por] = data;    
    
        return 0;
    }

    do I need add data.pNext=null in the function testData?

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If you look at DATAMAP, you'll see that this is a type definition. std::map<void*, TEST_D> is a type so you must first create an instance of that type to use it. Try something like:
    Code:
    DATAMAP d_map;
    d_map[por] = data;
    Edit:

    This makes your code compile:
    Code:
    #include <map>
    
    
    struct TEST_D
    {
        void* por;
        int nsize;
            void* pn;
        TEST_D* pNext;
    };
    
    
    typedef std::map<void*, TEST_D> DATAMAP;
    
    
    int testData(void* por,int nsize, void* pn = nullptr)
    {
        TEST_D data;
        data.por= por;
        data.nsize= nsize;
            data.pn= pn;
    
    
            //Uninitialized scalar variable (UNINIT) uninit_use: Using uninitialized value data. Field data.pNext is uninitialized
        DATAMAP d_map;
        d_map[por] = data;    
    
    
        return 0;
    }
    Last edited by MutantJohn; 06-16-2016 at 10:32 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > do I need add data.pNext=null in the function testData?
    Yes,
    data.pNext is uninitialised, so DATAMAP[por].pNext will be uninitialised as well.

    Anything outside this function isn't going to be able to determine whether DATAMAP[por].pNext means anything or not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. valgrind and "uninitialized" member values
    By MK27 in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2011, 11:14 AM
  3. Replies: 9
    Last Post: 09-14-2010, 07:16 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread