Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Uninitialized Variable in VC++

I have this piece of code,

#include <iostream>

void foo(int *a)
{
    std::cout<<*a<<std::endl;
}

int main()
{
    int i;
    foo(&i);
}

which is evident that an uninitialized variable is being used.

I have tried /RTCu as well as relied on error C4700, but for the above code, the compiler is not flagging this as an error.

I also tried running Code Analysis, bit it reported as the code fragment to have no issues.

So what is the reliable way to determine uninitialized variables in our source code?

I know valgrind does a wonderful JOB here but it is not an option for me as I have lots of calls to Windows APIs and MFC.

like image 767
Abhijit Avatar asked Nov 02 '22 15:11

Abhijit


1 Answers

There is no reliable way for a compiler determine all cases of uninitialized variables. Valgrind (and other tools like it) are not compilers, but dynamic analysis tools, like user gx above said. There are also static analysis tools that can detect many cases of use of unitialized variables. But generally compilers are no match for those specialized tools.

like image 192
zentrunix Avatar answered Nov 15 '22 05:11

zentrunix