Re:为什么人们说'extern'容易出错? [英] Re: Why do people say 'extern' is error prone ?

查看:77
本文介绍了Re:为什么人们说'extern'容易出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

4月11日下午6:06 * pm,Eri​​c Sosman< esos ... @ ieee-dot-org.invalidwrote:


Bartc写道:


[...]

但是举一个全局变量的例子(代码不是C):


byte winnt * * / *当这是WinNT时设置为1,否则设置为0 * /


我该怎么办呢?成千上万的功能需要

才能知道这个价值。



* * *我对那些认为所有全球

变量都是事实上邪恶的狂热者毫无耐心。 *当有人这样做时,我很高兴

提供


* * * * #include< stdio.h>

* * * * int main(无效){

* * * * * * puts(Hello,world!);

* * * * * *返回0;

* * * *}


...并邀请他消除全局变量,即

全局变量,其名称甚至没有提到!



prog output.txt

但我想当你最终查看数据时,全局变量

它会让它变得丑陋。


* * *然而,我觉得有理由避免全局变量。

他们像kudzu一样成长,它们会在模块之间产生意想不到的和不需要的b $ b耦合,它们可能是独立的,它们会使调试变得更加困难(我可以看到foo()崩溃,因为

全局变量条的值是无效的,但是如何在上帝的绿色中,b / b $ b $地球是如何被摧毁的?")*并且总有机会 -

就像我冗长的悲惨遭遇 - 你的代码将会被回收到单身人士非单数的情况下。

并且遇到一个奇点...


* * *我使用''em,但只有在没有证人的情况下。



追逐由于全局变量引起的问题(过去更糟糕的是,使用

Fortran公共区块,通常被视为公共工会)

很容易厌倦''他们。


他们并非都是邪恶的。只有他们中的大多数。

我讨厌的那些是完全不必要的,从懒惰中产生了

思考。

解决方案

到目前为止,在我的代码中,我没有使用过一个静态变量或一个const

变量(我更喜欢#defining常量并将它们放在

common.h文件)也是。我到目前为止从未觉得有必要这样做。我不知道C大师是否认为这是一个好习惯。


pereges写道:


到目前为止,在我的代码中,我还没有使用单个静态变量或const

变量(我更喜欢#defining常量并将它们放在一个地方在

common.h文件中)。我到目前为止从未觉得有必要这样做。我不知道C大师是否认为这是一个好习惯。



#define常量是维护程序员的祸害。他们通常无法在调试器中读取



-

Ian Collins。


pereges说:


到目前为止,在我的代码中,我还没有使用过一个静态变量或一个const

变量(我更喜欢#defining常量并将它们放在

common.h文件中的一个位置)。



#define在一些C程序员中获得了一个奇怪的声誉,

和一个我不太懂的。主要的反对意见似乎是,在调试器中,它们无法读取。


我刚在gdb中尝试过这个。我没有使用gdb很多,但是我知道/如何使用它,所以我写了以下程序:


#include < stdio.h>


#define X 42

int main(无效)

{

printf("%d \ n",X);

返回0;

}


并将程序加载到gdb中。


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

(gdb)运行

启动程序:[...] ./ foo


断点1,main()at foo.c:7

7 printf("%d \ n",X);

(gdb)print X

无符号X在当前的背景下。

+++++++++++++++++++++++++++++++++++++++++ br />

哦,亲爱的我。但是等等!


++++++++++++++++++++++++++++++++++++++++++++++ ++

(gdb)list

2

3 #define X 42

4

5 int main(无效)

6 {

7 printf("%d \ n",X);

8返回0;

9}

10

(gdb)

++++++++++ +++++++++++++++++++++++++++++++++++++++++++在原始源代码中查看

值。所以我真的不认为这是一个严重的反对意见。


我到目前为止从未觉得有必要这样做。我不知道C大师是否认为这是一个好习惯。



我认为这取决于你与谁交谈。 (这也取决于你认为是谁b / b $ g $ b。)


-

Richard Heathfield< http:/ /www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


On Apr 11, 6:06*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:

Bartc wrote:

[...]
But take this one example of a global variable (the code was not C):

byte winnt * */* set to 1 when this is WinNT, 0 otherwise */

What was I supposed to do with that? A few functions out of thousands need
to know that value.


* * *I have no patience with zealots who argue that all global
variables are ipso facto evil. *When someone does so, I delight
in offering

* * * * #include <stdio.h>
* * * * int main(void) {
* * * * * * puts("Hello, world!");
* * * * * * return 0;
* * * * }

... and inviting him to eliminate the global variable, that is,
the global variable whose name isn''t even mentioned!

prog output.txt
But I guess when you look at the data eventually, the global variable
will rear its ugly head.

* * *And yet, I feel there is reason to avoid global variables.
They grow like kudzu, they induce unexpected and unwanted
couplings between modules that could have been independent, they
make debugging harder ("I can see that foo() crashes because the
value of global variable bar is invalid, but how in God''s green
earth did bar get clobbered?") *And there''s always the chance --
as in my lengthy tale of woe upthread -- that your code will be
recycled into a situation where the Singleton is non-singular
and encounters a singularity ...

* * *I use ''em, but only when there are no witnesses.

After chasing down problems due to globals (it used to be worse, with
Fortran common blocks which were often treated like public unions)
it''s easy to get sick of ''em.

They are not all evil. Only most of them.
The ones I hate are the totally unnecessary ones, spawned from lazy
thinking.

解决方案

So far in my code, I haven''t used a single static variable or a const
variable(I prefer to #defining constants and have them at one place in
common.h file) either. I just never felt the need to do it so far. I
don''t know if the C gurus consider this as a good practice.


pereges wrote:

So far in my code, I haven''t used a single static variable or a const
variable(I prefer to #defining constants and have them at one place in
common.h file) either. I just never felt the need to do it so far. I
don''t know if the C gurus consider this as a good practice.

#define constants are the scourge of the maintenance programmer. They
are often impossible to read in a debugger.

--
Ian Collins.


pereges said:

So far in my code, I haven''t used a single static variable or a const
variable(I prefer to #defining constants and have them at one place in
common.h file) either.

#define has gained a strangely poor reputation amongst some C programmers,
and one that I don''t really understand. The principal objection seems to
be that, in a debugger, they are "impossible to read".

I just tried this in gdb. I don''t use gdb a great deal, but I know /how/ to
use it, so I wrote the following program:

#include <stdio.h>

#define X 42

int main(void)
{
printf("%d\n", X);
return 0;
}

and loaded the program into gdb.

+++++++++++++++++++++++++++++++++++++
(gdb) run
Starting program: [...]./foo

Breakpoint 1, main () at foo.c:7
7 printf("%d\n", X);
(gdb) print X
No symbol "X" in current context.
+++++++++++++++++++++++++++++++++++++

Oh deary deary me. But wait!

+++++++++++++++++++++++++++++++++++++
(gdb) list
2
3 #define X 42
4
5 int main(void)
6 {
7 printf("%d\n", X);
8 return 0;
9 }
10
(gdb)
+++++++++++++++++++++++++++++++++++++

Also, of course, we can simply look in the original source code to see the
value. So I really don''t see this as being a serious objection.

I just never felt the need to do it so far. I
don''t know if the C gurus consider this as a good practice.

It depends who you talk to, I think. (It also depends on whom you consider
to be gurus.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于Re:为什么人们说'extern'容易出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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