为什么在 for 循环中没有初始化的声明没有任何警告?
Why is there not any warning on a declaration without initialization in a for loop?
我尝试使用 g++(gcc 版本 4.8.2(Debian 4.8.2-1))编译以下代码,带有 -Wall
标志(添加 -Wextra
标志不会改变任何东西对我来说)。
#include <iostream>
using namespace std ;
int main() {
int i ;
cout << i << endl ;
}
它给出了这个警告:
test.cpp: In function ‘int main()’:
test.cpp:7:13: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
cout << i << endl ;
但是下面的代码不会产生任何警告:
#include <iostream>
using namespace std ;
int main() {
for(int i ; i < 10 ; i++) {
cout << i << endl ;
}
}
我做了进一步的测试。
以下会产生警告:
#include <iostream>
using namespace std ;
int main() {
int i ;
while(i<10) {
cout << i << endl ;
}
}
但以下不是:
#include <iostream>
using namespace std ;
int main() {
int i ;
while(i<10) {
cout << i << endl ;
i++ ;
}
}
在上面的程序中,如果我将 while
替换为 if
,则会出现警告。
有什么解释吗?为什么编译器在某些情况下可以识别问题,而在其他情况下却不能,尽管它们看起来非常接近?
感谢Pradhan who gave this link,我明白了问题。
此 link 声明如下:
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.
确实,当我添加这些标志之一时,编译器会发出警告。
我尝试使用 g++(gcc 版本 4.8.2(Debian 4.8.2-1))编译以下代码,带有 -Wall
标志(添加 -Wextra
标志不会改变任何东西对我来说)。
#include <iostream>
using namespace std ;
int main() {
int i ;
cout << i << endl ;
}
它给出了这个警告:
test.cpp: In function ‘int main()’:
test.cpp:7:13: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
cout << i << endl ;
但是下面的代码不会产生任何警告:
#include <iostream>
using namespace std ;
int main() {
for(int i ; i < 10 ; i++) {
cout << i << endl ;
}
}
我做了进一步的测试。
以下会产生警告:
#include <iostream>
using namespace std ;
int main() {
int i ;
while(i<10) {
cout << i << endl ;
}
}
但以下不是:
#include <iostream>
using namespace std ;
int main() {
int i ;
while(i<10) {
cout << i << endl ;
i++ ;
}
}
在上面的程序中,如果我将 while
替换为 if
,则会出现警告。
有什么解释吗?为什么编译器在某些情况下可以识别问题,而在其他情况下却不能,尽管它们看起来非常接近?
感谢Pradhan who gave this link,我明白了问题。
此 link 声明如下:
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.
确实,当我添加这些标志之一时,编译器会发出警告。