[英]GCC - no warning about an uninitialized array with -O0
My GCC 7.3.0 and 8.2.0 has some strange behavior I can't explain.我的 GCC 7.3.0 和 8.2.0 有一些我无法解释的奇怪行为。 This program, which obviously ends in a Segmentation fault:
该程序显然以分段错误结束:
int main()
{
double array[2]={0, 0};
printf("%f\n", array[999]);
return 0;
}
This is a long-standing, documented limitation in GCC.这是 GCC 中长期存在的、记录在案的限制。 Quoting the manual for GCC 3.0<\/a> :
引用
GCC 3.0 的手册<\/a>:
![]()
-Wuninitialized<\/code><\/strong>
<\/blockquote>
The
current version of the manual<\/a> has actually removed the second paragraph of this quotation, saying instead "Because these warnings depend on optimization, the exact variables or elements for which there are warnings depends on the precise optimization options and version of GCC used."
当前版本的手册<\/a>实际上已经删除了该引用的第二段,而是说“因为这些警告取决于优化,所以有警告的确切变量或元素取决于精确的优化选项和使用的 GCC 版本。” This is because, at some point between GCC 3.0 (released 2001) and GCC 8.2 (released 2018) the compiler was improved so that it will<\/em> give warnings for at least some uses of uninitialized variables when not optimizing.
这是因为,在 GCC 3.0(2001 年发布)和 GCC 8.2(2018 年发布)之间的某个时间点,编译器得到了改进,因此它会<\/em>在未优化时至少对未初始化变量的某些使用发出警告。 For instance, the trivial test
例如,琐碎的测试
int foo(void) { int x; return x; }<\/code><\/pre>
does provoke a warning when compiled with GCC 8.2 using
-O0 -Wall<\/code> .
使用 GCC 8.2 编译时确实会引发警告
-O0 -Wall<\/code> 。
It is worth pointing out that perfect<\/em> diagnosis of uninitialized variables reduces to the infamous
Halting Problem<\/a> —which means it can't be done.值得指出的是,对未初始化变量的完美<\/em>诊断会归结为臭名昭著的
停机问题<\/a>——这意味着它无法完成。 You can implement a set of rules that are conservatively correct (they will detect all uses of uninitialized variables, but they may claim that some variables are used uninitialized when they aren't), eg Java's definite assignment<\/a> rules, but that approach has historically been unpopular among C programmers.
您可以实现一组保守正确的规则(它们会检测所有未初始化变量的使用,但他们可能会声称某些变量在未初始化时未初始化使用),例如 Java 的
明确赋值<\/a>规则,但这种方法在历史上一直是在 C 程序员中不受欢迎。 Given a demand for minimal false positives but also quick compilation when not optimizing, GCC's approach of doing more elaborate analysis when optimization is on is understandable.
考虑到对最小误报的需求,但在不优化时也需要快速编译,GCC 在优化时进行更精细分析的方法是可以理解的。
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.