使用未初始化的值时,编译器可以给警告? [英] Can compilers give warnings when using uninitialised values?

查看:365
本文介绍了使用未初始化的值时,编译器可以给警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们可以说我不小心做出了愚蠢的错字..这个文件:

test.c的

 的#include<&stdio.h中GT;诠释的main()
{
    INT X = X;
    的printf(%d个\\ N,X);
}

编译罚款:

  mymachine上:〜$ OLL GCC test.c的
mymachine上:〜$ OLL ./a.out
1782198366

显然 INT X = X 是一个错误,但编译器接受这种毫无预兆。我已经浪费了不少时间试图此错误。

有没有这一点,可以使用GCC编译器标志/ G ++使编译器给我一个警告,当我使用一个未初始化堆栈变量?这可能会救了我大量的时间在未来。

我曾尝试 GCC -O -Wuninitialized test.c以 - 没有工作。

在此先感谢

编辑:我曾尝试 -Wall ,没有提到的x

  mymachine上:〜$ OLL GCC -Wall test.c的
test.c的:在函数'主':
test.c的:7:警告:控制​​到达非void函数结束


编辑:解决方案找到

看来,使用命令行工具 GCC G ++ 在OS X 10.8不给这个警告使用

  mymachine上:〜$ OLL铛-Wall test.c的
test.c的:5:10:警告:它自​​己的初始化中使用时变量'X'未初始化[-Wuninitialized]
        INT X = X;
        〜^
1警告产生。


解决方案

它看起来像的警告标志 -Wuninitialized -Winit自看直播 的):


  

警告有关与自己初始化未初始化的变量。注意此选项只能与-Wuninitialized选项一起使用。


  
  

例如,GCC发出警告我只当已指定-Winit自我下面的代码片段被未初始化的:


  INT F()
{
    INT I = I;
    返回我;
}


  

此警告被-Wall在C ++中启用。


根据评价下面可能有一些版本的依赖关系。请注意,生成警告这个只是用 -Wall ,这似乎更明智的对我说:

 警告:它自​​己的初始化中使用时变量'X'未初始化[-Wuninitialized]
INT X = X;
    〜^

我上面链接的活生生的例子还包括注释掉命令行。

另请参阅为什么-Winit自我从-Wuninitialized 分开。

So let's say I'm careless and make a stupid typo.. this file:

test.c

#include <stdio.h>

int main()
{
    int x = x;
    printf("%d\n",x);
}

compiles fine:

mymachine:~ oll$ gcc test.c 
mymachine:~ oll$ ./a.out 
1782198366

Obviously int x = x is a mistake but the compiler accepts this without warning. I've wasted quite a few hours trying to this error.

Is there a compiler flag that and can use for gcc / g++ to make the compiler give me a warning when I use an uninitialised stack variable? This could potentially save me a lot of time in the future.

I have tried gcc -O -Wuninitialized test.c - didn't work.

Thanks in advance

Edit: I have tried -Wall, no mention of x

mymachine:~ oll$ gcc -Wall test.c 
test.c: In function ‘main’:
test.c:7: warning: control reaches end of non-void function


Edit: Solution found

It seems that using the command line tools gcc and g++ in OS X 10.8 doesn't give this warning, using clang works:

mymachine:~ oll$ clang -Wall test.c
test.c:5:10: warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]
        int x = x;
        ~   ^
1 warning generated.

解决方案

It looks like the warning flags you want are -Wuninitialized -Winit-self (see it live):

Warn about uninitialized variables that are initialized with themselves. Note this option can only be used with the -Wuninitialized option.

For example, GCC warns about i being uninitialized in the following snippet only when -Winit-self has been specified:

int f()
{
    int i = i;
    return i;
}

This warning is enabled by -Wall in C++.

Based on the comments below there may be some version dependencies. Note, that clang generates a warning for this just using -Wall, which seems more sensible to me:

warning: variable 'x' is uninitialized when used within its own initialization [-Wuninitialized]
int x = x;
    ~   ^

The live example I linked above also includes a commented out clang command line.

Also see Why is -Winit-self separate from -Wuninitialized.

这篇关于使用未初始化的值时,编译器可以给警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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