变量声明风格 [英] variable declaration style

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

问题描述

让我们说我有这样的代码,哪个更好?


for(;;)

{

bool完成;


do

{

}

while(!finished);

}





bool完成;


for (;;)

{


do

{

}

while(!finished);

}


每当声明变量时,我倾向于认为编译器的作用。

如果编译器在找到变量

定义时从堆栈中分配怎么办?

Let''s say I had code like these, what is better?

for(;;)
{
bool finished;

do
{
}
while (!finished);
}

or

bool finished;

for(;;)
{

do
{
}
while (!finished);
}

Whenever declaring variables I tend to think like what a compiler does.
What if a compiler allocates from the stack whenever it finds a variable
definition?

推荐答案

John Doe写道:
John Doe wrote:

让我们说我有这样的代码,哪个更好?
Let''s say I had code like these, what is better?



前者。简单的规则是:除非某些其他要求阻止

,否则请保留您声明的任何变量的最紧密范围。

The former. The simple rule is: unless some other requirement prevents
it, keep the tightest scope for any variable you declare.


>

for(;;)

{

bool完成;


do

$

}

while(!finished);

}





bool完成;


for(;;)

{




{

}

while(!finished);

}

每当声明变量时,我倾向于像编译器那样思考
。如果编译器在找到

变量定义时从堆栈中分配怎么办?
>
for(;;)
{
bool finished;

do
{
}
while (!finished);
}

or

bool finished;

for(;;)
{

do
{
}
while (!finished);
}

Whenever declaring variables I tend to think like what a compiler
does. What if a compiler allocates from the stack whenever it finds a
variable definition?



那么?它没有指定,编译器可以自由地执行它看到的任何内容

fit。无论如何,你为什么要关心?


V

-

请在回复时删除资本''A'电子邮件

我没有回复最热门的回复,请不要问

So? It''s not specified, the compiler is free to do whatever it sees
fit. Why do you care, anyway?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


前者。简单的规则是:除非某些其他要求阻止
The former. The simple rule is: unless some other requirement prevents

,否则请保留您声明的任何变量的最严格范围。
it, keep the tightest scope for any variable you declare.



谢谢

thanks


所以?它没有指定,编译器可以自由地执行它看到的任何内容

fit。无论如何,你为什么要关心?


V
So? It''s not specified, the compiler is free to do whatever it sees
fit. Why do you care, anyway?

V



因为所有类型的分配都需要时间,比如10000s堆栈

分配可能会影响执行时间。它认为它是未指定的b $ b,但是经验法则是什么?现代C ++编译器

通常做什么?

because all kinds of allocation take time and, say 10000s of stack
allocations could make an impact on execution time. It figures that it''s
unspecified, but what is the rule-of-thumb? What do modern C++ compilers
usually do?


John Doe写道:

[snip]
John Doe wrote:
[snip]

因为所有类型的分配都需要时间,比如10000s的堆栈

分配会对执行时间产生影响。它认为它是未指定的b $ b,但是经验法则是什么?现代C ++编译器通常做什么?b $ b?
because all kinds of allocation take time and, say 10000s of stack
allocations could make an impact on execution time. It figures that it''s
unspecified, but what is the rule-of-thumb? What do modern C++ compilers
usually do?



他们优化了它。


- J.

They optimize that away.

- J.


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

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