取消引用nullptr时,编译器不会发出警告

The*_* do 2 c++ null compiler-warnings dereference

这个:

int* p = nullptr;
auto tmp = *p;  
Run Code Online (Sandbox Code Playgroud)

不会导致gcc 4.6和VS2010 sp1至少发出警告.在这些编译器中是否有任何选项可以让它们在这种情况下发出警告?我使用/ w4在VS中进行了测试编译.

orl*_*rlp 8

这不违法,只是未定义的行为.

打开此警告可能会导致错误的安全感,因为通常直到运行时才会知道某些指针指向nullptr或指向其他内容.Valgrind可以检查这些错误(还有更多).

更不用说它会大大增加编译时间.


Ore*_*ost 5

在Visual Studio 2010 SP1中(不知道您需要哪个版本),运行代码分析.我刚刚尝试使用您的确切代码,它显示警告6011:

warning C6011: NULL-Zeiger "p" wird dereferenziert.: Lines: 138, 139
Run Code Online (Sandbox Code Playgroud)