为什么g ++对未初始化变量的警告取决于变量的类型? (它警告一个int但不是一个双) [英] Why does g++ warning about uninitialized variable depend on the type of the variable? (it warns for an int but not for a double)

查看:240
本文介绍了为什么g ++对未初始化变量的警告取决于变量的类型? (它警告一个int但不是一个双)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图理解在哪些情况下g ++警告未初始化的变量。考虑下面的代码:

I'm currently trying to understand in which cases g++ warns about uninitialized variables. Consider the following piece of code:

#include <iostream>

typedef double barType;

struct foo {

    barType bar;

};

int main() {

    foo f;

    std::cout << f.bar << std::endl;

}



如果我这样编译, / p>

If I compile it like this I get no warning:

$ g++ -O1 -Wall test.cc -o test

但如果我将barType更改为int:

but if I change barType to int:

$ g++ -O1 -Wall test.cc -o test
  test.cc: In function ‘int main()’:
  test.cc:17: warning: ‘f.foo::bar’ is used uninitialized in this function

警告如何取决于类型?在这两种情况下都未初始化。

How can the warning depend on the type? It is uninitialized in both cases.

我正在使用:

$ g++ --version
g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

感谢,

>

推荐答案

这是未定义的行为,不需要诊断,所以编译器可以自由判断。他们本可以做得更好。

It's undefined behavior, which is not required to be diagnosed, so the compiler is free to make its judgement. They could have done better.

这篇关于为什么g ++对未初始化变量的警告取决于变量的类型? (它警告一个int但不是一个双)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆