[LC++]declaring and using manipulating global variables

Dr Mark H Phillips mark at austrics.com.au
Fri May 16 10:57:02 UTC 2003

On Fri, 2003-05-16 at 11:44, Julien Patrick Claassen wrote:
> Hi!
>   My unhappiness. I want to use a global variable for counting up an
> array-index. But now all functions that deal with my array have to get it as
> a parameter. That makes me unhappy, because most of them, don't even do
> something with it directly (they may just pass it along for another
> "subfunction" they call. Besides, I thought there was a way to declare a
> global variable, that is known everywhere and can be changed everwhere. Being
> changed meaning, changing it globally. I simply tried it with something like:
> #include <something>
> int glob_index;
> (functions here using glob_index);
> int main()
> {
>   glob_index = 6;
>   change_glob_index(); // being a function that manipulates glob_index
>   return 0;
> }
>   I used some simple output to see the value of glob_index and saw, that it is
> only changed locally.

I can't see how that could be.  If "glob_index" is a global variable
(which is how you have defined it above) then if you change it inside
"change_glob_index()" then it will remain changed when you return from
this call.  Can you provide us with a simple example illustrating what
happens, complete with program output?

>   Is there a - let's say - more elegant way of doing this? Or is my way the
> usual?

Global variables are sometimes the "quick and easy" way to do something
but often far from the best.  For small programs it is probably okay to
use them, but for larger ones it can cause problems.

The problem is that with global variables it is difficult to see which
functions affect what variables.  A better strategy is: with any
variable you wish to view or change within a function, pass it in as a
parameter; this means one needs only to look at the parameter list to
see what potentially could change.  Having this coding policy means it's
much easier to scan code and work out what code affects what.

As you have observed however, passing a parameter around everywhere
because it is used everywhere, is a pain.  But if you truly need it
to be "global", it is in my opinion a pain worth putting up with.  The
question is however: 

  do you really need this global variable?

You say that "glob_index" is for counting up an array index.  Why do
you need this count?  Does this mean the array it is indexing is global
too?  What are you using this array for?  Does it really need to be seen
by everything?



>   Kindest regards
>         Julien
> Julien Patrick Claassen
> jclaassen at gmx.de
> julien at c-lab.de
> http://www.geocities.com/jjs_home
> Fuerstenallee 11
> 33102 Paderborn
> Phone: (+49) 5251 60 6060
> Fax: (+49) 5251 60 6065
> www.c-lab.de
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
Dr Mark H Phillips
Research Analyst (Mathematician)

AUSTRICS - smarter scheduling solutions - www.austrics.com

Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
Phone +61 8 8226 9850
Fax   +61 8 8231 4821
Email mark at austrics.com.au

More information about the tuxCPProgramming mailing list