Re: Global variables!

From: sms (smsutton_at_iwon.com)
Date: 05/05/04


Date: Wed, 05 May 2004 20:59:12 GMT


"Atmapuri" <janez.makovsek@usa.net> wrote in message
news:z5llc.1950$37.277194@news.siol.net...
> Hi!
>
> > Well I guess...but when it comes to global variables, people must be
> warned
> > about their usage. All my teachers have said to try and limit the use of
> > globals, so do most textbooks I've read. After actually having
programmed
> > for about a year now I see the point.
>
> The application has to maintain its state. The only way to do that in C is
> with global variables. In C++ you use global "objects". An alternative
> would be to define a struct in the main program and then pass it by
> reference to all functions within the program. I must say
> prefere an #include at the begining of each source file.
>
> Thanks for the hints.
> Atmapuri.

In C, have you considered:

In file var1.c

static int my_var1 = 0;

void
set_var1(int value)
{
    my_var1 = value;
}

int
get_var1(void)
{
    return my_var1;
}

If your compiler supports inline functions in C, you can
make the files inline functions, which reduces the
overhead.

It is a bit less convient that variables, but it does
provide some data hiding.

I must admit I tend to used it more for flags, with
the function names something like modified() and
isModified() to track data structures that need to
be written back to files.
Stanley