我对一些代码运行cppcheck进行了检查,以寻找可能的运行时错误。它报告了以下情况可能存在空指针解引用:
Foo* x = ... //defined somewhere
...
Foo* y(x); //possible null pointer dereference.
编辑:更好的例子
for( int i = 0; i < N; i++ )
{
Foo* x( ArrayOfObjsContainingFooPtr[i].FooPtr ); // line 3
if( !x ) // line 4
continue;
}
来自cppcheck的错误信息:
[C:\file.cpp:3]: (错误) 可能的空指针解引用:x - 否则在第4行检查x是否为空是多余的
但我不知道这怎么可能发生。