gpt4 book ai didi

c++ - gcc - 删除 "is used uninitialized in this function"警告

转载 作者:IT王子 更新时间:2023-10-29 00:38:11 28 4
gpt4 key购买 nike

在将 -O2 -Wall 标志添加到 gcc (4.4.6) 后清除警告代码。我在一些遗留代码中有很多警告。这是演示问题的非常简化的版本:

 1 #include <cstdio>
2
3 bool init(bool& a)
4 {
5 return true;
6 }
7
8 int main()
9 {
10 bool a;
11
12 if (!init(a))
13 {
14 return 1;
15 }
16
17 if (a)
18 {
19 printf("ok\n");
20 }
21 }

将其编译为“gcc main.cpp -O2 -Wall”时,我收到:

 main.cpp:17: warning: `a' is used uninitialized in this function

在实际代码中,init() 只有在初始化“a”时才返回 true,因此实际上没有使用未初始化的“a”。

可以采取哪些措施来修复警告。

最佳答案

bool a; 更改为 bool a = false; 将删除此警告。

编译器不会知道 init(a) 是为了“初始化 a”,它只会看到程序试图用未初始化的变量调用函数。

关于c++ - gcc - 删除 "is used uninitialized in this function"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374122/

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