地方变量宣言的风格 [英] Style of Local Variable Declaration

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

问题描述

您更喜欢哪种样式的局部变量声明;将所有内容

放在函数顶部或仅在使用它的块内?


例如;


void

fn(struct foo * f,int bar)

{

struct abc d;

int i;


abc_init(& d);


for(i = 0; i< bar; i ++){

unsigned long num;

struct eee * e;


for(e = f-> a; e< f-> alim; e ++){

time_t time time_now;

struct tm tm_now;

struct blah b;

...





void

fn(struct foo * f,int bar)

{

struct abc d;

int i;

unsigned long num;

struct eee * e;

time_t time time_now;

struct tm tm_now;

struct blah b;


abc_init(& d);


for(i = 0; i< bar; i ++){

for(e = f-> a; e< f-> alim; e ++){

...


我倾向于使用第二种形式,因为相信堆栈指针频繁移动会导致效率低下但我没有想法。如果我的直觉是真的。


迈克

解决方案



" Michael B Allen < MB ***** @ ioplex.com>在消息中写道

news:pa ********************************** @ ioplex .c om ...

您更喜欢哪种风格的局部变量声明;将所有内容放在函数顶部或仅在使用它的块中?


如果变量'的目的在逻辑上适合特定区块,

那么我会在那里声明它。

例如;

fn(struct foo * f,int bar)
{
struct abc d;
int i;

abc_init(& d);

for(i = 0; i< bar; i ++){
unsigned long num;
struct eee * e; < (e = f-> a; e< f-> alim; e ++){
time_t time time_now;
struct tm tm_now;
struct blah b;



fn(struct foo * f,int bar)
{
struct abc d;
int i;
unsigned long num;
struct eee * e;
time_t time time_now;
struct tm tm_now;
struct等等;(i = 0; i< bar; i ++){
for(e = f-) > a; e< f> alim; e ++){
...

我倾向于使用第二种形式,因为相信堆栈指针的频繁移动会导致效率低下,但我不知道如果我的直觉是真的。


为什么会暗示?

Mike



Michael B Allen写道:

您更喜欢哪种风格的局部变量声明;将所有内容放在功能的顶部或仅在使用它的区域内?

(剪辑代码示例)


第一个一,确切地说。

你应该将变量的可见性限制在最可能受限制的

区块

......
<我倾向于使用第二种形式,因为相信堆栈指针的频繁移动会导致效率低下,但如果我的直觉是真的,我就没有想法。



我不确定,但我想根据

实施和优化选项,这将大不相同。但无论如何我甚至都不会关心这个问题。我更喜欢写清楚,可读,如果可能的话

强大的代码。


布鲁诺


在消息< pa ********************************** @ ioplex.com>

Michael B Allen< mb ***** @ ioplex.com>写道:

您更喜欢哪种风格的局部变量声明;将所有内容放在功能的顶部或仅在使用它的区域内?


< snip>

我''我倾向于使用第二种形式,因为相信堆栈指针的频繁移动会导致效率低下,但如果我的直觉是真的,我就没有想法。




两种方式都有效。通过限制变量的生命周期,你可能会减少堆栈和注册使用,无论生命周期是否重叠。

但是,在某些实现中,堆栈是当你进出块时,指针可能会上下移动。但堆栈指针移动可能很便宜,

如果它们不是实现可能不会这样做。


和智能编译器可能能够找出哪些变量是不相交的,并允许它们共享寄存器/内存(实时范围分割),

,无论你如何声明它们。


我真的不会太担心效率 - 只需担心

C代码风格。如果我担心,我倾向于使用有限范围的

声明,而不是功能范围的声明。


-

Kevin Bracey,首席软件工程师

Tematic Ltd电话:+44(0)1223 503464

182-190 Newmarket Road传真:+44(0)1223 503458

剑桥,CB5 8HE,英国WWW: http://www.tematic .com /


Which style of local variable declaration do you prefer; put everything
at the top of a function or only within the block in which it is used?

For example;

void
fn(struct foo *f, int bar)
{
struct abc d;
int i;

abc_init(&d);

for (i = 0; i < bar; i++) {
unsigned long num;
struct eee *e;

for (e = f->a; e < f->alim; e++) {
time_t time time_now;
struct tm tm_now;
struct blah b;
...

OR

void
fn(struct foo *f, int bar)
{
struct abc d;
int i;
unsigned long num;
struct eee *e;
time_t time time_now;
struct tm tm_now;
struct blah b;

abc_init(&d);

for (i = 0; i < bar; i++) {
for (e = f->a; e < f->alim; e++) {
...

I''ve tended to use the second form based on the belief that frequent
movement of the stack pointer would lead to inefficiencies but I have no
idea if my instinct is true.

Mike

解决方案


"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...

Which style of local variable declaration do you prefer; put everything
at the top of a function or only within the block in which it is used?
If the variable''s purpose fits logically within a specific block,
then I would declare it there.

For example;

void
fn(struct foo *f, int bar)
{
struct abc d;
int i;

abc_init(&d);

for (i = 0; i < bar; i++) {
unsigned long num;
struct eee *e;

for (e = f->a; e < f->alim; e++) {
time_t time time_now;
struct tm tm_now;
struct blah b;
...

OR

void
fn(struct foo *f, int bar)
{
struct abc d;
int i;
unsigned long num;
struct eee *e;
time_t time time_now;
struct tm tm_now;
struct blah b;

abc_init(&d);

for (i = 0; i < bar; i++) {
for (e = f->a; e < f->alim; e++) {
...

I''ve tended to use the second form based on the belief that frequent
movement of the stack pointer would lead to inefficiencies but I have no
idea if my instinct is true.

Why would it imply that?
Mike



Michael B Allen wrote:

Which style of local variable declaration do you prefer; put everything
at the top of a function or only within the block in which it is used?
(snip code exemples)

The first one, definitively.
"Thou shall limit variables visibility to the most possible restricted
block"
...

I''ve tended to use the second form based on the belief that frequent
movement of the stack pointer would lead to inefficiencies but I have no
idea if my instinct is true.



I''m not sure, but I guess this will greatly differ according to the
implementation and the optimization options. But I would not even care
about this anyway. I prefer to write clear, readable and if possible
robust code.

Bruno


In message <pa**********************************@ioplex.com >
Michael B Allen <mb*****@ioplex.com> wrote:

Which style of local variable declaration do you prefer; put everything
at the top of a function or only within the block in which it is used?

<snip>
I''ve tended to use the second form based on the belief that frequent
movement of the stack pointer would lead to inefficiencies but I have no
idea if my instinct is true.



It works both ways. By limiting the lifetime of variables you''re potentially
reducing stack and register usage whereever the lifetimes don''t overlap.
But yes, in some implementations, the stack pointer might move up and down as
you move in and out of blocks. But stack pointer moves are probably cheap,
and if they weren''t the implementation probably wouldn''t do it.

And a smart compiler may well be able to figure out which variables are
disjoint and allow them to share registers/memory (live range splitting),
regardless of how you declare them.

I really wouldn''t worry about efficiency too much - just worry about the
C code style. If I was worrying, I would tend to go with limited-scope
declarations, not function-scope ones.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/


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

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