全局变量不好? [英] global variables are bad?

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

问题描述

为什么呢?我听说过这个,不鼓励使用全球变量而不是b $ b当地人,但为什么呢?

thx :)

Why? i'' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?
thx :)

推荐答案

fabio schrieb:
fabio schrieb:
为什么?我听说过这个,不鼓励使用全球变量而不是当地人,但为什么呢?
Why? i'' ve heard about this, the usage of global vars instead of
locals is discouraged, but why?




没有全球性的东西C中的变量。

您可能的意思是具有静态存储持续时间的变量

和外部链接。为什么要对面额这么大惊小怪呢?

存储持续时间和链接决定了

global的不同方面。


链接:外部链接(暗示文件)范围和)意味着

标识符(变量名称)必须是唯一一个在整个程序中具有完全

这个名称和链接的标识符。只要

在范围内有变量声明,它就是可见的

并可在相应的范围内访问。这可以是另一个

翻译单位;这是什么意思?你正在使用变量

作为内部状态记录,现在有人,甚至你自己,b / b
,不假思索地或者看似巧妙地覆盖了这个

状态。这可能会在一些更改后轻易扰乱您的

程序的内部工作。

您现在可以降低变量的可见性和可访问性

by给它内部链接(声明保留在文件级别

但前面加上静态),这意味着它只能在

一个翻译单元中访问 - 只要因为你不给它的地址

外面的程序的某些部分 - 或者通过给它块区域和

没有链接(声明在一个函数和之前是

静态)这意味着它只能从其声明中获取

结束} - 只要你不给它的地址

外部程序的某些部分。


存储持续时间:静态存储持续时间意味着你的变量

在整个生命周期中都存在你的程序。这是

只初始化一次并且在两次写入

访问之间保持其值。这怎么可能不好?

1)你的程序的哪一部分直接知道变量或

至少知道它的地址,可以修改它。

2)如果一个函数使用这个变量来跟踪某种内部状态并且只预期某些状态转换,那么

从两个不同的点调用该函数在你的程序中

可以扰乱函数的逻辑并导致无效的结果。如果该函数是库的一部分,那么

尤其是一个问题

并且这个库可以被多个程序访问,或者如果你是
有一个多线程程序 - 这种情况不包含在

标准C中,所以我不会详细介绍。阅读reentrancy

以了解所涉及的问题。


此外,如果您的计划中有多个地方
使用或修改变量的
,即使你们都仔细计划了它并且没有上述情况,你也可以更容易地做出b $ b使用

这个变量设计算法时的错误比你刚刚以严格控制的方式传递它或其地址时的错误。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



There is no such thing as a global variable in C.
What you probably mean is a variable with static storage duration
and external linkage. Why make such a fuss about denomination? The
storage duration and the linkage determine different sides of
"global".

Linkage: External linkage (implies file scope and) means that the
identifier (the "variable name") must be the only one with exactly
this name and linkage throughout the whole programme. As soon as
there is a declaration of the variable in scope, it is "visible"
and accessible in the respective scope. This can be another
translation unit; what does this mean? You are using the variable
for an internal status record and now someone, maybe even you
yourself, thoughtlessly or seemingly cleverly overwrites this
status. This can easily upset the internal workings of your
programme after some changes.
You now can reduce the visibility and accessibility of the variable
by giving it internal linkage (the declaration remains on file level
but is preceded by "static") which means it is only accessible within
one translation unit -- as long as you do not give its address to
some part of the programme outside --, or by giving it block scope and
no linkage (the declaration is within a function and preceded by
"static") which means it is only accessible from its declaration to
the closing } -- as long as you do not give its address to
some part of the programme outside.

Storage duration: Static storage duration means that your variable
exists throughout the whole life-time of your programme. It is
initialized only once and holds its value between two writing
accesses. How can this be bad?
1) Whatever part of your programme directly knows the variable or
at least knows its address, can modify it.
2) If a function uses this variable to keep track of some sort of
internal status and expects only certain status transitions, then
calling the function from two different points in your programme
can upset the function''s logic and lead to invalid results. This
can be especially an issue if the function is part of a library
and this library is accessed by more than one programme or if you
have a multithreaded programme -- this situation is not covered by
standard C, so I will not go into detail. Read up on "reentrancy"
to understand the issues involved.

In addition, if you have more than one place in your programme
where a variable is used or modified, even if you all planned it
out carefully and have none of the situations described above, you
can more easily make a mistake when designing the algorithm using
this variable than when you just pass it or its address around in
a strictly controlled manner.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Richard G. Riley说:
Richard G. Riley said:
" Michael"在2006-03-11发布了以下内容:
"Michael"posted the following on 2006-03-11:

在C中没有全局变量这样的东西。

There is no such thing as a global variable in C.



http:// www。 google.com/search?q=global+variables+in+c



http://www.google.com/search?q=tooth+fairy (近四百万次点击)。

只是因为它在Google上,并不代表它存在。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



http://www.google.com/search?q=tooth+fairy (nearly four million hits).

Just cos it''s on Google, doesn''t mean it exists.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


" Richard Heathfield"写道:
"Richard Heathfield" wrote:
http: //www.google.com/search?q=tooth+fairy (将近四百万次点击)。

只是在Google上,它并不代表它存在。
http://www.google.com/search?q=tooth+fairy (nearly four million hits).

Just cos it''s on Google, doesn''t mean it exists.




我个人最喜欢的,abraham lincoln 量子物理学 68K点击。

http://www.google.com/search?hl=en&q...=Google+Search


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

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