Afficher un message
Vieux 11/04/2008, 21h40   #5
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why do people say 'extern' is error prone ?

Bartc said:

<snip>

> Is this the argument against using global variables?


No, this is the polemic against file scope objects with external likage.
The argument against global variables is down the corridor, third door on
the left.

> I might be in trouble then. My last proper application used I think
> between 1000 and 2000 global variables, shared amongst all modules. And
> that doesn't include file-scope variables in each module, perhaps another
> 1000.


Then yes, you're in trouble. You might take a while to notice, it's true.
If you haven't already had problems, they'll hit during maintenance.

> I suppose many could have been collected in a giant struct which is then
> passed everywhere, but it sounds silly to collect unrelated variables
> like that.
>
> And some could be accessed by functions instead, although that just
> substitutes global functions for global variables.
>
> What exactly is the problem with global variables?


Assuming you mean file scope objects with external linkage: as well as
always being there when you want them, they're always there when you don't
want them. They bind modules too tightly to each other, making code re-use
difficult. They mean that, when you step over an apparently pure
(no-pointer-args) function call in a debugger, you don't know whether your
program state has changed underneath you.

Seriously, if you're bright enough to cope with 2000 file scope objects
with external linkage, well, good luck to you. Myself, I'd prefer to keep
things modular.

--
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
  Réponse avec citation
 
Page generated in 0,07089 seconds with 9 queries