为什么我没有得到“使用未初始化的”在这个微不足道的例子中,来自gcc的警告? [英] why am I not getting an "used uninitialized" warning from gcc in this trivial example?

查看:993
本文介绍了为什么我没有得到“使用未初始化的”在这个微不足道的例子中,来自gcc的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决序列反转程序中的这种分割错误?



所以我打算重复请使用 - Wall flags评论,但是当我测试代码与警告时,我发现没有警告报告给我很大的惊喜。

所以我修剪它(这段代码对于执行的目的没有任何意义,但它说明了我想展示的内容):

  #include< ; stdio.h中> 

int main()
{

int i,len = 12;

/ * printf(%d \ n,i); * /

while(i!= len-1)
{

i ++;
len--;
}

返回0;
}

使用 gcc 4.7.3和6.2.1使用

  gcc -Wall -Wextra -pedantic 

我没有得到任何警告,而 i 在使用<$ $之前没有初始化c $ c> while 循环。



现在,如果我取消注释 printf get:

 警告:'i'在此函数中未初始化使用[-Wuninitialized] 

那么为什么当传递 i printf时发出警告但不在中,而测试?



(它不同于 gcc无法警告未初始化的变量,因为在我的情况下,没有分支)



(听起来像一个bug,但它太微不足道了,我不知道如果我没有遗漏一些巨大的东西)。

解决方案

很难说它是一个 bug ,因为 gcc 将优化和创建警告的代码混合在一起,甚至为这个警告提供了警告:


-Wuninitialized

如果在不初始化的情况下使用自动变量,或者如果变量可能被setjmp调用破坏。
[...]

因为这些警告依赖于优化,所以存在警告的确切变量或元素取决于所使用的精确优化选项和GCC版本。 (来自GCC文档 Options.htmlrel =nofollow noreferrer>请求或取消警告的选项,强调我的)



你在这里找到了一个恕我直言的非常愚蠢的情况。试试 -O1 ,你会得到一个意想不到的警告:
$ b

  warn.c:在函数'main'中:
warn.c:13:6:警告:'i'可能在此函数中未初始化使用[-Wmaybe-uninitialized]
我++;

因此, gcc 仍然错过了第一次未初始化的使用,但找到第二个!试试 -O0 -O2 ,警告再次消失......



您仍然可以尝试提出有关此问题的错误。注意 clang 正确:
$ b

  warn.c:10:9:warning:变量'i'在这里使用时未初始化
[-Wuninitialized]
while(i!= len-1)
^
warn .c:6:8:注意:初始化变量'i'以使此警告消失
int i,len = 12;
^
= 0
1生成警告。


Once again a stupid uninitialized variable error in How to fix this segmentation error in a sequence inverting program?.

So I was going to repeat the "please use -Wall flags" comment, but when I tested the code against warnings, I found no warnings reported to my great surprise.

So I trimmed it down to this below (this code makes no sense for execution purposes but it illustrates what I want to show):

#include <stdio.h>

int main()
{

  int i,len=12;

  /* printf("%d\n",i); */

  while(i!=len-1)
  {

    i++;
    len--;
  }

 return 0;
 }

when compiling it using gcc 4.7.3 and 6.2.1 using

gcc -Wall -Wextra -pedantic

I get no warnings, whereas i is blatantly not initialized before using in the while loop.

Now if I uncomment the printf statement I get:

warning: 'i' is used uninitialized in this function [-Wuninitialized]

So why is the warning issued when passing i to printf but not in the while test?

(It's different of gcc failing to warn of uninitialized variable because in my case, there's are no branches)

(Sounds like a bug, but it's so trivial that I wonder if I'm not missing something huge.)

解决方案

It's hard to say it's a bug, because gcc mixes up code for optimization and for creating warnings, which is even documented for this particular warning:

-Wuninitialized
Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by a setjmp call. [...]
Because these warnings depend on optimization, the exact variables or elements for which there are warnings depends on the precise optimization options and version of GCC used.

(from GCC docs Options to Request or Suppress Warnings, emphasis mine)

You found an IMHO very silly case here. Try -O1 and you'll get an unexpected warning:

warn.c: In function ‘main’:
warn.c:13:6: warning: ‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     i++;
      ^

So, gcc still misses the first uninitialized use, but finds the second one! Try -O0 or -O2 and the warning is again gone...

You could still try to file a bug about this. Note clang gets it right:

warn.c:10:9: warning: variable 'i' is uninitialized when used here
      [-Wuninitialized]
  while(i!=len-1)
        ^
warn.c:6:8: note: initialize the variable 'i' to silence this warning
  int i,len=12;
       ^
        = 0
1 warning generated.

这篇关于为什么我没有得到“使用未初始化的”在这个微不足道的例子中,来自gcc的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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