[vox-tech] global variables in C

Mark K. Kim vox-tech@lists.lugod.org
Fri, 20 Sep 2002 12:16:13 -0700 (PDT)


On Fri, 20 Sep 2002, Peter Jay Salzman wrote:

==8<--
> conventional wisdom says that you should avoid using globals.

It's a rule of thumb.  It's not like a "goto"... :)

==8<--
> 1. name clashing
> 2. readability

Both good reasons.  These reasons aren't really relevant, though, if:

   1. Your program is small.
   2. A big, well-documented library uses the global variable.

As a sidenote, the name clashing problem (a.k.a., "name space" problem)
and the readability problem is one reason why OOP is useful -- you can
group functions that use global variables together.

> what i'd like to know is if this is all there is.  are there more
> reasons why we should follow conventional wisdom?

A few other things:

   - Globals can (but not necessarily, though usually does if the
     global variable is very big) add to your binary's file size.

   - Globals can (but not necessarily) speed up your program.
     (This is one reason why you might *want* to use globals, though,
     second to the reason that globals are always initialized to zero
     with no overhead.)

   - Sharing a global can be a problem.  If, for example, if you
     need to modify the variable from one function, then another function,
     then you realize they can't both use the variable in certain
     sequences, so you end up twisting your code to make sure that
     particular sequence doesn't occur, which it does thanks to Murphy,
     so your program ends up being unstable...

==8<--
> it seems
> like for some applications, like GUI and multi-thread programming, the
> use of globals can really simplify things a lot.
==8<--

well... yes... but only for things that are used everywhere, like the main
frame, mutex locks, etc...  Globals under multi-threaded programming can
be a horror if you don't keep a tight control.

On Fri, 20 Sep 2002, Peter Jay Salzman wrote:

==8<--
>    most of the functions have absurdly long names to begin with.  i know
>       you've seen xlib functions.  imagine them with the prefix "gtk_".
>
>    there are so many objects that you end up having to call them long
>       names just to keep it straight, like "upper_left_radio_button"
>
>    whenever you pass upper_left_radio_button, you almost always need to
>       typecast it to help C with typechecking.  so you almost never pass
>       upper_left_radio_button, you actually pass something like
>       GTK_OBJECT(upper_left_radio_button).  35 characters for a stupid
>       radio button!   :-)
==8<--

Keep local variable names short, and keep global variable names long.  A
word of wisdom from Kernighan...

As a sidenote, all the horrible typecasting required in C is one reason
why I think OO language is more suitable for GUI programming (the window
hierarchy is checked automatically via inheritance).  But that's neither
here nor there, I suppose.

-Mark

--
Mark K. Kim
http://www.cbreak.org/
PGP key available upon request.