静态与自动 [英] static vs auto

查看:61
本文介绍了静态与自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况,我必须选择

a静态变量和全局范围的自动变量

的文件


例如,


void main()

{

int a; ........................... 1

static int a; ..................... 2

}


使用是否有任何明显优势1比2,反之亦然。

I have situation like this, I have to choose between
a static variable and automatic variable with global scope
of a file

Eg.,

void main()
{
int a; ........................... 1
static int a; ..................... 2
}

Is there any significant advantage of using 1 over 2 or vice-versa.

推荐答案

aruna< ar ******** @ yahoo.co.in>这样说了:
aruna <ar********@yahoo.co.in> spoke thus:
我有这样的情况,我必须选择
一个静态变量和一个全局范围的自动变量
一个文件


两个变量都没有文件范围。

void main()


int main(void)。这是一个录音。

{
int a; ........................... 1
static int a; ..................... 2
}
使用1超过2还是有任何显着优势,反之亦然。
I have situation like this, I have to choose between
a static variable and automatic variable with global scope
of a file
Neither variable has file scope.
void main()
int main(void). This is a recording.
{
int a; ........................... 1
static int a; ..................... 2
} Is there any significant advantage of using 1 over 2 or vice-versa.




他们是相同的,除非你打算递归调用main()。


-

克里斯托弗Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



They''re identical unless you plan to call main() recursively.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
int main(void)。这是一个录音。

aruna< ar ******** @ yahoo.co.in>这样说:
int main(void). This is a recording.

aruna <ar********@yahoo.co.in> spoke thus:
{
int a; ........................... 1
static int a; ..................... 2
}
{
int a; ........................... 1
static int a; ..................... 2
}


使用是否有任何明显优势1比2,反之亦然。
Is there any significant advantage of using 1 over 2 or vice-versa.



除非你打算以递归方式调用main(),否则它们是相同的。



They''re identical unless you plan to call main() recursively.




不,他们可以有不同的行为,例如如果'a''

的地址存储在具有静态存储持续时间的对象中,则在main()之后由atexit()注册的函数访问它是


返回。在这种情况下,1是未定义的,但2是明确定义的。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}



No, they can have different behavior e.g. if the address of `a''
is stored in an object with static storage duration and it is
accessed by a function registered with atexit() after main() has
returned. In that case 1 is undefined but 2 is well-defined.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Ben Pfaff< bl*@cs.stanford.edu>这样说:
Ben Pfaff <bl*@cs.stanford.edu> spoke thus:
不,他们可以有不同的行为,例如如果'a''
的地址存储在具有静态存储持续时间的对象中,并且在main()返回后由atexit()注册的函数访问它。在这种情况下,1是未定义的但是2是明确定义的。
No, they can have different behavior e.g. if the address of `a''
is stored in an object with static storage duration and it is
accessed by a function registered with atexit() after main() has
returned. In that case 1 is undefined but 2 is well-defined.




它们是完全相同的,但是一个比
更相同
其他? ;)谢谢,当然。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



How about "They''re identical, but one is more identical than the
other"? ;) Thanks, of course.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于静态与自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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