为什么没有在for循环中进行初始化时没有任何警告? [英] Why is there not any warning on a declaration without initialization in a for loop?

查看:414
本文介绍了为什么没有在for循环中进行初始化时没有任何警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着用g ++(gcc version 4.8.2(Debian 4.8.2-1))编译下面的代码,用 -Wall 标志(添加<$

  #include  -Wextra  flag不会改变任何东西)。的iostream> 

使用namespace std;

int main(){
int i;
cout<<我<<末日
}

它给出了这个警告:

  test.cpp:在函数'int main()'中:
test.cpp:7:13:警告:'i'在此函数中未初始化[ -Wuninitialized]
cout<<我<<末日

但是下面的代码不会产生任何警告:

  #include< iostream> 

使用namespace std; (int i; i <10; i ++){
cout<<我<<末日
}
}

我做了进一步的测试。
$ b

以下是警告:

  #include< iostream> 

使用namespace std;

int main(){
int i;
,而(i <10){
cout<<我<<末日
}
}

但以下不会:

  #include< iostream> 

使用namespace std;

int main(){
int i;
,而(i <10){
cout<<我<<末日
i ++;






$ p在上面的程序中,如果我替换,而 if if ,那么我有一个警告。



有没有一些解释呢?为什么编译器能够在某些情况下识别问题,而不是在其他情况下识别问题,尽管它们看起来非常接近?解析方案

感谢 Pradhan 谁给了这个链接,我明白了这个问题。



这个链接指出:


GCC可以警告用户使用未初始化变量的值。这样的价值是不确定的,它从来没有用。它甚至没有用作随机值,因为它很少是一个随机值。不幸的是,在一般情况下,检测何时使用未初始化的变量是等价的,以解决暂停问题。 GCC尝试使用优化器收集的信息来检测某些实例,并在命令行中给出-Wuninitialized选项时警告它们。目前的实施存在一些可察觉的缺陷。首先,它仅在通过-O1,-O2或-O3启用优化时才有效。其次,根据启用的优化,误报或负值的集合会有所不同。这也会导致在发布之间添加或修改优化时报告的警告的高度可变性。


的确,当我添加其中一个标志,编译器会发出警告。


I tried to compile the following code using g++ (gcc version 4.8.2 (Debian 4.8.2-1)), with -Wall flag (adding the -Wextra flag does not change anything for me).

#include <iostream>

using namespace std ;

int main() {
    int i ;
    cout << i << endl ;
}

It gave this warning:

test.cpp: In function ‘int main()’:
test.cpp:7:13: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
    cout << i << endl ;

But the following code does not yield any warning:

#include <iostream>

using namespace std ;

int main() {
    for(int i ; i < 10 ; i++) {
        cout << i << endl ;
    }
}

I did further tests.

The following yields the warning:

#include <iostream>

using namespace std ;

int main() {
    int i ;
    while(i<10) {
        cout << i << endl ;
    }
}

But the following does not:

#include <iostream>

using namespace std ;

int main() {
    int i ;
    while(i<10) {
        cout << i << endl ;
        i++ ;
    }
}

In the above program, if I replace the while by an if, then I have a warning.

Is there some explanation to this? Why can the compiler recognize the problem in some cases and not in others, although they seem very close?

解决方案

Thanks to Pradhan who gave this link, I understood the problem.

This link states the following:

GCC has the ability to warn the user about using the value of a uninitialized variable. Such value is undefined and it is never useful. It is not even useful as a random value, since it rarely is a random value. Unfortunately, detecting when the use of an uninitialized variable is equivalent, in the general case, to solving the halting problem. GCC tries to detect some instances by using the information gathered by optimisers and warns about them when the option -Wuninitialized is given in the command line. There are a number of perceived shortcomings in current implementation. First, it only works when optimisation is enabled through -O1, -O2 or -O3. Second, the set of false positives or negatives varies according to the optimisations enabled. This also causes high variability of the warnings reported when optimisations are added or modified between releases.

Indeed, when I add one of these flags, the compiler yields the warning.

这篇关于为什么没有在for循环中进行初始化时没有任何警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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