何时初始化局部变量? [英] When to initialize the local variables?

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

问题描述




这两个中的哪一个是初始化本地

变量的更好方法?为什么?


1)在申报时。


例如:


void func()

{

int i = 0;


/ *部分代码如下* /

i =< actual_value_of_i> ;;

}

2)就在使用之前。


例如:


void func()

{

int i;


/ *部分代码如下* /

i =< actual_value_of_i> ;; / *i现已初始化* /

}


请咨询。


谢谢,

Sriram。

Hi,

Which of these two would be a better way of initializing the local
variables? and why?

1) At the time of their declaration.

Eg:

void func()
{
int i =0;

/* Some code follows */
i = <actual_value_of_i>;
}
2) Just before their use.

Eg:

void func()
{
int i;

/* Some code follows */
i = <actual_value_of_i>; /* "i " is initialized now */
}

Please advice.

Thanks,
Sriram.

推荐答案



Sriram Rajagopalan写道:

Sriram Rajagopalan wrote:




这两个中的哪一个是初始化本地

变量的更好方法?为什么?


1)在申报时。


例如:


void func()

{

int i = 0;


/ *部分代码如下* /

i =< actual_value_of_i> ;;

}


2)就在使用之前。


Eg :


void func()

{

int i;


/ *部分代码遵循* /

i =< actual_value_of_i> ;; / *i现已初始化* /

}


请咨询。


谢谢,

斯里兰姆。
Hi,

Which of these two would be a better way of initializing the local
variables? and why?

1) At the time of their declaration.

Eg:

void func()
{
int i =0;

/* Some code follows */
i = <actual_value_of_i>;
}
2) Just before their use.

Eg:

void func()
{
int i;

/* Some code follows */
i = <actual_value_of_i>; /* "i " is initialized now */
}

Please advice.

Thanks,
Sriram.



初始化变量时没关系,只要你没有

读取变量并使用它''初始化之前的s值。

第二种情况可能是最好的,因为在第一种情况下你是两次初始化我的b $ b,这是多余的。并不是说这真的很重要

无论如何,因为编译器可能会优化这种冗余,大概可能是
。但是,如果您在

声明时知道i的值,那么为什么不在那时呢?这真的是

的味道,也取决于具体情况。


MQ

It doesn''t matter when you initialize a variable, as long as you do NOT
read the variable and use it''s value before it is initialized. The
second case is probably the best, since in the first case you are
initializing i twice, which is redundant. Not that this really matters
anyway, since the compiler would optimize such redundancy out most
likely. However, if you know the value of i at the point of
declaration, why not do it at that point? It really is a matter of
taste and also depends on the situation.

MQ

Sriram Rajagopalan写道:
Sriram Rajagopalan wrote:




这两个中的哪一个是更好的初始化方法当地

变量?为什么?


1)在申报时。


例如:

void func()

{

int i = 0;

/ *有些代码如下* /

i =< actual_value_of_i> ;

}


2)就在使用之前。


例如:

void func()

{

int i;

/ *部分代码如下* /

i =< ; actual_value_of_i取代; / *i现已初始化* /

}


请咨询。
Hi,

Which of these two would be a better way of initializing the local
variables? and why?

1) At the time of their declaration.

Eg:
void func()
{
int i =0;
/* Some code follows */
i = <actual_value_of_i>;
}

2) Just before their use.

Eg:
void func()
{
int i;
/* Some code follows */
i = <actual_value_of_i>; /* "i " is initialized now */
}

Please advice.



方法1.导致更安全的代码,方法2.导致可以忽略不计的更快

代码。方法2.可能会导致代码可读性更低,或者可能不是b $ b,这取决于程序员的口味。

Method 1. leads to safer code, method 2. leads to negligibly faster
code. Method 2. might lead to negligibly more readable code, or might
not, depending on the programmer''s tastes.


Sriram Rajagopalan写道:
Sriram Rajagopalan wrote:




这两个中的哪一个是初始化本地的更好方法/>
变量?为什么?


1)在申报时。


例如:


void func()

{

int i = 0;


/ *部分代码如下* /

i =< actual_value_of_i> ;;

}


2)就在使用之前。


Eg :


void func()

{

int i;


/ *部分代码遵循* /

i =< actual_value_of_i> ;; / *i现已初始化* /

}


请咨询。
Hi,

Which of these two would be a better way of initializing the local
variables? and why?

1) At the time of their declaration.

Eg:

void func()
{
int i =0;

/* Some code follows */
i = <actual_value_of_i>;
}
2) Just before their use.

Eg:

void func()
{
int i;

/* Some code follows */
i = <actual_value_of_i>; /* "i " is initialized now */
}

Please advice.



(Advi / s / e。建议是给出的;建议是做什么的。)


以上两个例子都没有。


(3)当他们在声明中声明他们的实际价值时。


void func ()

{

int i =< i的实际值> ;;

}


这在C99中是最简单的,它突然发生了理智的攻击,并且在声明之后允许

声明,但即使在C90中你也可以非常接近

而不会丑化你的声明代码[1]。然后它只是需要在阅读时思考的特殊情况。


[1]太多了。

-

克里斯微妙,像桶一样 Dollin

我还在这里,我正在拿着答案 - 卡纳塔克邦,/爱与感情/

("Advi/s/e". Advice is what gets given; advise is what gets done.)

Neither of the above examples.

(3) When they are declared, in their declaration, to their actual value.

void func()
{
int i = <actual value of i>;
}

This is easiest in C99, which had a sudden attack of sanity and allowed
declarations after statements, but even in C90 you can get pretty close
to this without uglifying your code [1]. Then it''s only the exceptional
cases that need thinking at read-time.

[1] Too much.

--
Chris "subtle, like a barrel" Dollin
"I''m still here and I''m holding the answers" - Karnataka, /Love and Affection/


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

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