Reputation: 63737
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.
Upvotes: 5
Views: 591
Reputation: 563
Regarding the static analysis portion: As of VS2013, I would not depend on the default analyzers to catch too much, if you have access to another static analysis tool (although it is good to use VS to catch what others might miss). Although it is likely not the case here, note that static analyzers may disregard patterns that they see in many places or that have been overridden by other developers as the acceptable norm. I would be surprised if Coverity did not report this case, though (sorry, don't have it installed ATM for testing). If it did ignore it, an explanation might be that it was considered a possible shared memory address.
Upvotes: 1
Reputation: 2270
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.
Upvotes: 1