gpt4 book ai didi

我可以强制 gcc 检测所有未定义的行为吗?

转载 作者:行者123 更新时间:2023-11-30 19:54:21 24 4
gpt4 key购买 nike

有没有办法强制 gcc 检测所有未定义的行为?我希望它能够检测到可以在编译时和运行时发现的东西。我知道 UB 对于简化编译器的创建和允许编译器优化代码都很有用。后者在调试时并不相关,而且对轻量级编译器的需求也不像 1972 年那么大。此外,gcc 目前是一个非常成熟的编译器,如果这是可能的,那么调试会变得非常困难更容易。

我知道 -Wformat 会对 printf("%d", 42) 和未初始化的变量产生警告。当您尝试访问数组外部的内存时,参数 -Warray-bounds 可能 会被捕获,尽管我需要在构建实际产生警告的代码方面做一些工作。我还知道可以使用 -fstack-protector-all 检测到一些运行时错误。

所以我的问题很简单。有没有办法保证所有UB都能被检测到,如果可能的话在编译时,但最晚在运行时发生?

最佳答案

这是不可能的。检测未定义的行为实际上需要解决停止问题;例如,引用 C11 6.8.5 :

6 An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.

C 的设计目的并不是为了使错误检测变得容易。

关于我可以强制 gcc 检测所有未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55759044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com