【发布时间】:2016-03-09 00:42:12
【问题描述】:
gcc 版本-gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
使用 -Wall -Wuninitialized 编译下面的代码会按预期抛出警告 警告:在此函数中未初始化使用“测试”[-Wuninitialized]
#include<stdio.h>
#include<stdlib.h>
void check (int test )
{
printf("%d",test);
}
int
main()
{
int test;
check(test);
return 0;
}
但是使用 -Wall -Wuninitialized 编译下面的代码不会引发警告
#include<stdio.h>
#include<stdlib.h>
void check (int test )
{
printf("%d",test);
}
int
main()
{
int test;
int condition = 0;
if(condition == 27)
test = 10;
check(test);
return 0;
}
它不应该抛出警告吗?是否与编译器优化有关?
【问题讨论】:
-
标准没有要求编译器必须警告这种类型的未定义行为。
-
测试 = 10;我想它会使用变量,即使有一个路径单元化。
-
根据here,gcc 从版本 4.9 开始对此发出警告。你也许可以升级你的编译器版本。
-
@M.M 我有 4.9.3 和 5.3.0 没有警告,即使有所有这些标志。