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

发布于 2024-11-05 07:35:48 字数 163 浏览 6 评论 0原文

这:

int* p = nullptr;
auto tmp = *p;  

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

This:

int* p = nullptr;
auto tmp = *p;  

doesn't cause neither gcc 4.6 nor VS2010 sp1 to issue at least a warning. Is there any option in any of those compilers to make them issue a warning in cases like this? I test-compiled in VS with /w4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

可爱暴击 2024-11-12 07:35:48

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

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

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

It's not illegal, it's just undefined behaviour.

Turning on warnings for this might cause a false sense of security, because often it is not known until runtime if some pointer points to nullptr or to something else. Valgrind can check for these errors (and many more).

Not to mention it would dramatically increase compile times.

绅士风度i 2024-11-12 07:35:48

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

warning C6011: NULL-Zeiger "p" wird dereferenziert.: Lines: 138, 139

In Visual Studio 2010 SP1 (don't know which edition you need), run Code Analysis. I just tried with your exact code, and it shows warning 6011:

warning C6011: NULL-Zeiger "p" wird dereferenziert.: Lines: 138, 139
淡淡離愁欲言轉身 2024-11-12 07:35:48

“像这样的情况”非常模糊,它需要编译器从每个取消引用中回溯,看看是否可以证明它知道指针具有常量且无效的值。

考虑到程序的其他部分可以被赋予指向该指针的指针(别名)并在不同的代码路径中覆盖它,或者(更糟糕)在不同的线程上覆盖它。

这并不容易检测到,而且我认为,即使可以通过合理的努力来实现,实际程序的编译时间成本也会使其不值得。

"Cases like this" are quite obscure, and it would require the compiler to backtrack from every de-reference and see if it can prove that it knows that the pointer has a constant, and invalid, value.

Consider that some other piece of the program could be given a pointer to the pointer (aliasing) and overwrite it in a different code path, or (worse) on a different thread.

It's not easy to detect, and I think the cost in terms of compilation time for real-world programs would make it not worth it, if it even could be implemented with a reasonable amount of effort.

溺深海 2024-11-12 07:35:48

valgrind 可以指出这一点。我用它来检查这种模糊的情况以及无效的读/写。

valgrind can point this out. I use it to check for this kind of obscure cases, and invalid reads/writes.

怀里藏娇 2024-11-12 07:35:48

这是运行时错误,而不是编译时错误。编译器仅在非常有限的情况下才能捕获此类取消引用,在这种情况下,编译器可以完全确定指针值。增加复杂性是没有意义的。

This is runtime error, not compile time error. Compiler can catch such dereferences only in very limited cases where it can be completely sure you about pointer value. There is just no point for added complexity.

迷雾森÷林ヴ 2024-11-12 07:35:48

这是一种未定义的行为。这意味着编译器不需要发出警告或错误。

That is an undefined behaviour. That means the compiler is not required to issue a warning or error.

芸娘子的小脾气 2024-11-12 07:35:48

取消引用空指针是未定义的行为。编译器可以诊断此类情况,但不强制要求这样做。

Dereferencing a null pointer is undefined behavior. The compiler is allowed to diagnose such cases but is not required to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文