初始化变量 [英] Initialising Variables

查看:108
本文介绍了初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是习惯于在声明时初始化变量,然后一些

的同事开始告诉我这是不好的做法,编译器

应该留下来发现使用uninitilised变量因此

可能的错误。


您对上述内容的想法将受到欢迎(作为旁白),但我的主要内容

问题如下。


现在我最近升级到gcc 4,我发现我错过了编译器

警告我以前有关未初始化的变量的gcc 3(警告

标志 - 墙等等,仍然存在,我只是没有收到警告信息)。

请考虑以下代码。我希望得到一个警告,关于foo是未初始化的,但我不是。我肯定有一个简单的解释,但是我不能看到它。$ / b

int

main(无效)

{

int foo;

int i;


i = 0;

while(i){

if(i == 10){

foo = 50;

}


如果(foo){

printf("%d \ n",foo);

}


++ i;

if(i 20){

break;

}

}

返回0;

}

I always used to initialise variables at declaration, then a couple of
colleagues started telling me it was bad practice and that the compiler
should be left to spot the use of uninitilised variables and hence
possible bugs.

Your thoughts on the above would be welcome (as an aside), but my main
problem follows.

Now I''ve recently upgraded to gcc 4, and I find I''m missing the compiler
warnings I used to get on gcc 3 regarding uninitialized vars (the warning
flags -Wall, etc., are still there, I just don''t get the warning messages).

Consider the code below. I expected to get a warning about foo being
uninitialized, but I don''t. I''m sure there''s a simple explanation, but I
can''t see it.

int
main(void)
{
int foo;
int i;

i = 0;
while (i) {
if (i == 10) {
foo = 50;
}

if (foo) {
printf("%d\n", foo);
}

++i;
if (i 20) {
break;
}
}
return 0;
}

推荐答案



DaveC写道:

DaveC wrote:

我总是习惯在声明时初始化变量,然后是几个

的同事开始告诉我这是不好的做法,并且应该留下编译器

来发现uninitilised变量的使用因此

可能的错误。
I always used to initialise variables at declaration, then a couple of
colleagues started telling me it was bad practice and that the compiler
should be left to spot the use of uninitilised variables and hence
possible bugs.



我不相信标准规定编译器必须为此生成

警告。

I don''t believe the standard states that compilers have to produce
warnings for this.


您对以上内容的看法会受到欢迎(作为旁白),但我的主要

问题随之而来。


现在我'最近升级到gcc 4,我发现我错过了编译器

警告我曾经在gcc 3上关于未初始化的vars(警告

标志) -Wall等等,仍然存在,我只是没有收到警告信息)
Your thoughts on the above would be welcome (as an aside), but my main
problem follows.

Now I''ve recently upgraded to gcc 4, and I find I''m missing the compiler
warnings I used to get on gcc 3 regarding uninitialized vars (the warning
flags -Wall, etc., are still there, I just don''t get the warning messages)



这是一个关于GCC编译器的问题,而不是关于C

语言。这可能不是理想的新闻组,可以问这样的

作为问题。


< Off-topic>

两个海湾合作委员会网页上的会议记录产生了
http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options

和-Wuninitialized选项,由启用 ; -Wall",但取决于-O1上的
(至少)。

< / Off-topic>

This is now a question about the GCC compiler, not about the C
language. This is probably not the ideal newsgroup in which to ask such
as question.

<Off-topic>
Two minutes on the GCC webpages yielded
http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options
and the -Wuninitialized option which is enabled by "-Wall", but depends
on -O1 (at least).
</Off-topic>


DaveC写道:
DaveC wrote:

我总是习惯于在声明时初始化变量,然后一些

的同事开始讲述我这是不好的做法,编译器

应留下来发现未经过消毒的变量的使用,因此

可能的错误。


您对以上内容的想法会受到欢迎(作为旁白),但主要的

问题随之而来。

现在我最近升级到gcc 4,我发现我错过了编译器

警告我曾经在gcc 3上找到关于未初始化的变量的警告<警告

flags -Wall等,仍然存在,我只是没有收到警告信息)。
I always used to initialise variables at declaration, then a couple of
colleagues started telling me it was bad practice and that the compiler
should be left to spot the use of uninitilised variables and hence
possible bugs.

Your thoughts on the above would be welcome (as an aside), but my main
problem follows.

Now I''ve recently upgraded to gcc 4, and I find I''m missing the compiler
warnings I used to get on gcc 3 regarding uninitialized vars (the warning
flags -Wall, etc., are still there, I just don''t get the warning messages).



这真的不是这个地方;在gcc特定的新闻组或

邮件列表中询问。

This is really not the place; ask in a gcc-specific newsgroup or
mailing list.


>

请考虑以下代码。我希望得到一个警告,关于foo是未初始化的,但我不是。我肯定有一个简单的解释,但是我不能看到它。$ / b

int

main(无效)

{

int foo;

int i;


i = 0;

while(i){

if(i == 10){

foo = 50;

}


如果(foo){

printf("%d \ n",foo);

}


++ i;

if(i 20){

break;

}

}


返回0;

}
>
Consider the code below. I expected to get a warning about foo being
uninitialized, but I don''t. I''m sure there''s a simple explanation, but I
can''t see it.

int
main(void)
{
int foo;
int i;

i = 0;
while (i) {
if (i == 10) {
foo = 50;
}

if (foo) {
printf("%d\n", foo);
}

++i;
if (i 20) {
break;
}
}
return 0;
}



整个while循环是死代码。当前gcc在数据流分析通过之前执行DCE(死代码

消除)传递,这是产生未初始化变量警告的传递




回到主题上,请注意C编译器需要__not__来检测或诊断此问题。这是一个实施的质量,或者问题。 gcc开发人员选择尽最大努力警告用户使用
,他们只针对输入二进制文件中将出现的非死代码进行操作。这避免了浪费非平凡的时间

分析输出二进制文件中永远不会存在的代码。


将代码更改为i = 1,编译再看看你得到了什么。


Mark F. Haigh
mf ** ***@sbcglobal.net

Your entire while loop is dead code. Current gccs do a DCE (dead code
elimination) pass before the data flow analysis pass, which is the pass
that produces the uninitialized variable warnings.

Back somewhat on-topic, note that C compilers are __not__ required to
detect or diagnose this issue. It''s a quality of implementation, or
QoI, issue. The gcc developers choose to make a best effort to warn
the user, and they only do it for non-dead code that will be present in
the output binary. This avoids wasting non-trivial amounts of time
analyzing code that will never even exist in the output binary.

Change the code to i = 1, compile again, and see what you get.

Mark F. Haigh
mf*****@sbcglobal.net


On Mon,2007年1月22日03:18:25 -0800,mark_bluemel写道:
On Mon, 22 Jan 2007 03:18:25 -0800, mark_bluemel wrote:

>

DaveC写道:
>
DaveC wrote:

>现在我最近升级到gcc 4,我发现我错过了编译器
警告我曾经在gcc 3上关于未初始化的vars(警告
标志-Wall等,仍然存在,我只是没有收到警告信息)
>Now I''ve recently upgraded to gcc 4, and I find I''m missing the compiler
warnings I used to get on gcc 3 regarding uninitialized vars (the warning
flags -Wall, etc., are still there, I just don''t get the warning messages)



这是关于GCC编译器的问题,而不是关于C

语言的问题。这可能不是理想的新闻组,可以问这样的

作为问题。


< Off-topic>

两个海湾合作委员会网页上的会议记录产生了
http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options

和-Wuninitialized选项,由启用 ; -Wall",但取决于-O1上的
(至少)。

< / Off-topic>


This is now a question about the GCC compiler, not about the C
language. This is probably not the ideal newsgroup in which to ask such
as question.

<Off-topic>
Two minutes on the GCC webpages yielded
http://gcc.gnu.org/onlinedocs/gcc-4....arning-Options
and the -Wuninitialized option which is enabled by "-Wall", but depends
on -O1 (at least).
</Off-topic>



是的,我意识到自己有脱离主题的危险,但感谢

回复。为了记录,使用了那些警告标志,以及

优化:(


Yeah, I realised I was in danger of being off topic, but thanks for the
response anyway. For the record, those warning flags were used, as was
the optimization :(


这篇关于初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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