gpt4 book ai didi

c - gcc -Wall -Wuninitialized 不会对未初始化的变量发出警告

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:20 26 4
gpt4 key购买 nike

gcc 版本 - gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

使用 -Wall -Wuninitialized 编译以下代码会按预期抛出警告警告:“test”在此函数中使用未初始化 [-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;
}

它不应该发出警告吗?它与编译器优化有关吗?

最佳答案

What an user understands as a false positive may be different for the particular user. Some users are interested in cases that are hidden because of actions of the optimizers combined with the current environment. However, many users aren't, since that case is hidden because it cannot arise in the compiled code. The canonical example is (MM05):

int x;
if (f ())
x = 3;
return x;

where 'f' always return non-zero for the current environment, and thus, it may be optimised away. Here, a group of users would like to get an uninitialized warning since 'f' may return zero when compiled elsewhere. Yet, other group of users would consider spurious a warning about a situation that cannot arise in the executable being compiled.

https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings#Proposal


编辑:MMO5 已经修复

https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings#MM05

关于c - gcc -Wall -Wuninitialized 不会对未初始化的变量发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880871/

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