gcc 编译器忽略调试构建的未初始化变量警告
gcc compiler ignores uninitialized variable warning for debug build
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;
}
gcc a.c -g -Wall -Werror
无警告
gcc a.c -O3 -Wall -Werror
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 对于 -Wmaybe-uninitialized
gcc 选项:
-Wmaybe-uninitialized
For an automatic (i.e. local) variable, if there exists a path from the function entry to a use of the variable that is initialized, but there exist some other paths for which the variable is not initialized, the compiler emits a warning if it cannot prove the uninitialized paths are not executed at run time.
These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables.
我猜是因为分析未初始化变量的代价对于不优化编译来说太大了。这就是为什么它只是为了优化一个。
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;
}
gcc a.c -g -Wall -Werror
无警告
gcc a.c -O3 -Wall -Werror
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 对于 -Wmaybe-uninitialized
gcc 选项:
-Wmaybe-uninitialized
For an automatic (i.e. local) variable, if there exists a path from the function entry to a use of the variable that is initialized, but there exist some other paths for which the variable is not initialized, the compiler emits a warning if it cannot prove the uninitialized paths are not executed at run time.
These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables.
我猜是因为分析未初始化变量的代价对于不优化编译来说太大了。这就是为什么它只是为了优化一个。