gcc
编译器将忽略uninitialized variable warning
进行调试。这对我来说看起来很奇怪,有人可以帮助我理解吗?
## Program
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, max;
int count;
if (argc < 2) {
return -1;
}
max = atoi(argv[1]);
for (i = 0; i < max; i++) {
count++;
}
printf("count is %d\n", count);
return 0;
}
无警告
a.c: In function ‘main’:
a.c:8:9: error: ‘count’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
int count;
^~~~~
cc1: all warnings being treated as errors
gcc
版本:7.4.0
尽管看起来可能很奇怪,但对于documented gcc选项,此行为是-Wmaybe-uninitialized
:
-Wmaybe-uninitialized
对于自动(即本地)变量,如果存在从函数入口到使用该变量的路径,而该路径已初始化,但是存在其他一些未初始化该变量的路径,则编译器会在以下情况下发出警告:它不能证明未初始化的路径在运行时未执行。
这些警告仅在优化编译时才可能出现,因为否则GCC不会跟踪变量的状态。
我想原因是,对未初始化变量进行分析的成本对于不优化编译而言太高了。这就是为什么只对一个进行优化的原因。