l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
August 5: Social gathering
Next Installfest:
TBD
Latest News:
Jul. 4: July, August and September: Security, Photography and Programming for Kids
Page last updated:
2002 Sep 20 13:13

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] global variables in C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] global variables in C



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.


_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Sunset Systems
Who graciously hosts our website & mailing lists!