This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CGT 6.1.23 failure to warn of uninitialized variable.

I've found what I consider to be a nefarious deficiency in the C++ compiler - especially since I have recently spent over a day looking for a particular bug I composed. The problem with the compiler is illustrated by the following test case:

int TestCase (void)
{   int  uninitialized_1;
    int  uninitialized_2;

    for (int i=0; i<1; i++)
    {   uninitialized_1++;
    }
    uninitialized_2 += 1;

    return uninitialized_1 + uninitialized_2;
}

For the above code, the compiler warns that the variable 'uninitialized_2' is used before its value is set, but does not provide a similar warning for the variable 'uninitialized_1'. The lack of warning seems to be related to the variable residing within the loop body. If one simply comments the for-loop declaration (as below), the compiler correctly emits a pair of warnings, one for each of the uninitialized variables. Does anyone know if this behavior been fixed in a later version of the tool-set?

int TestCase (void)
{   int  uninitialized_1;
    int  uninitialized_2;

    //for (int i=0; i<1; i++)
    {   uninitialized_1++;
    }
    uninitialized_2 += 1;

    return uninitialized_1 + uninitialized_2;
}

  • The TI compiler does not warn about the first case because of the loop. Note that g++ 4.9.2 also misses this case. Although we can look at it and know that it enters the loop uninitialized, the part of the compiler that does the uninitialized variable checking doesn't understand more than the fact that it is in a loop. That part of the compiler was not designed to be aggressive; in general, it's a difficult problem, and it can take a long time. There are other tools available from other vendors to check this sort of thing aggressively. We're unlikely to make this check more aggressive in the compiler.