Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #151048 > unrolled thread

Ways to avoid global variables

Started byMehdi Amini <atorrses@gmail.com>
First post2020-04-05 13:10 +0430
Last post2020-04-06 20:37 +0100
Articles 5 on this page of 25 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  Ways to avoid global variables Mehdi Amini <atorrses@gmail.com> - 2020-04-05 13:10 +0430
    Re: Ways to avoid global variables Mehdi Amini <atorrses@gmail.com> - 2020-04-05 13:23 +0430
    Re: Ways to avoid global variables felix@palmen-it.de (Felix Palmen) - 2020-04-05 11:11 +0200
    Re: Ways to avoid global variables Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 02:34 -0700
    Re: Ways to avoid global variables James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 10:48 -0400
      Re: Ways to avoid global variables James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 11:43 -0400
        Re: Ways to avoid global variables James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 16:49 -0400
        Re: Ways to avoid global variables Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-05 16:12 -0700
      Re: Ways to avoid global variables James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 11:49 -0400
        Re: Ways to avoid global variables Mehdi Amini <atorrses@gmail.com> - 2020-04-06 14:11 +0430
          Re: Ways to avoid global variables Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-06 12:08 +0100
    Re: Ways to avoid global variables David Brown <david.brown@hesbynett.no> - 2020-04-05 22:03 +0200
      Re: Ways to avoid global variables James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-05 16:53 -0400
        Re: Ways to avoid global variables Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 14:11 -0700
        Ice cream (Was: Ways to avoid global variables) gazelle@shell.xmission.com (Kenny McCormack) - 2020-04-06 06:45 +0000
        Re: Ways to avoid global variables David Brown <david.brown@hesbynett.no> - 2020-04-06 09:19 +0200
      Re: Ways to avoid global variables Mehdi Amini <atorrses@gmail.com> - 2020-04-06 13:30 +0430
        Re: Ways to avoid global variables Spiros Bousbouras <spibou@gmail.com> - 2020-04-06 09:25 +0000
          Re: Ways to avoid global variables Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-06 03:03 -0700
            Re: Ways to avoid global variables Spiros Bousbouras <spibou@gmail.com> - 2020-04-06 10:16 +0000
              Re: Ways to avoid global variables Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-06 03:21 -0700
                Re: Ways to avoid global variables Spiros Bousbouras <spibou@gmail.com> - 2020-04-06 10:50 +0000
                Re: Ways to avoid global variables David Brown <david.brown@hesbynett.no> - 2020-04-06 13:46 +0200
    Re: Ways to avoid global variables scott@slp53.sl.home (Scott Lurndal) - 2020-04-06 18:05 +0000
      Re: Ways to avoid global variables Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-06 20:37 +0100

Page 2 of 2 — ← Prev page 1 [2]


#151123

FromMalcolm McLean <malcolm.arthur.mclean@gmail.com>
Date2020-04-06 03:21 -0700
Message-ID<7bb46e39-8f29-4f8e-8368-fb35f88f2f69@googlegroups.com>
In reply to#151122
On Monday, 6 April 2020 11:16:57 UTC+1, Spiros Bousbouras  wrote:
> On Mon, 6 Apr 2020 03:03:02 -0700 (PDT)
> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> > On Monday, 6 April 2020 10:25:24 UTC+1, Spiros Bousbouras  wrote:
> > > 
> > > > I would say without /static/ keyword.
> > > 
> > > Seems like a reasonable definition to me and the kind of thing I would
> > > understand intuitively as a global variable.
> > >
> > Say we've got this
> > 
> > static int numberbase = 10;
> > 
> > void setnumberbase(int base)
> > {
> >    if(base < 2)
> >      numberbase = 2;
> >    else if(base > 16)
> >      numberbase = 16;
> >    else
> >      numberbase = base;
> > }
> > 
> > int getnumberbase(void)
> > {
> >    return numberbase;
> > }
> > 
> > Our variable is protected - it can't go out of range. But it's 
> > globally visible. Anyone who includes our header can set the 
> > numberbase for the whole program.
> 
> Function definitions should not go into headers. So you would put
> in a header
> 
> void setnumberbase(int) ;
> int getnumberbase(void) ;
> 
> and  #include  this in files which need to access  numberbase .So
> what's the problem , what point were you trying to make ?
>
We've got rid a global variable with external linkage. And we
haven't achieved nothing, we've prevented numberbase from being set
to a value we can't handle. But have we really done anything 
significant as far as addressing the problems of global variables
goes?

[toc] | [prev] | [next] | [standalone]


#151126

FromSpiros Bousbouras <spibou@gmail.com>
Date2020-04-06 10:50 +0000
Message-ID<ssZzTvD3PmfqiLepdZ@bongo-ra.co>
In reply to#151123
On Mon, 6 Apr 2020 03:21:31 -0700 (PDT)
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On Monday, 6 April 2020 11:16:57 UTC+1, Spiros Bousbouras  wrote:
> > On Mon, 6 Apr 2020 03:03:02 -0700 (PDT)
> > Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> > > Say we've got this
> > > 
> > > static int numberbase = 10;
> > > 
> > > void setnumberbase(int base)
> > > {
> > >    if(base < 2)
> > >      numberbase = 2;
> > >    else if(base > 16)
> > >      numberbase = 16;
> > >    else
> > >      numberbase = base;
> > > }
> > > 
> > > int getnumberbase(void)
> > > {
> > >    return numberbase;
> > > }
> > > 
> > > Our variable is protected - it can't go out of range. But it's 
> > > globally visible. Anyone who includes our header can set the 
> > > numberbase for the whole program.
> > 
> > Function definitions should not go into headers. So you would put
> > in a header
> > 
> > void setnumberbase(int) ;
> > int getnumberbase(void) ;
> > 
> > and  #include  this in files which need to access  numberbase .So
> > what's the problem , what point were you trying to make ?

> We've got rid a global variable with external linkage. And we
> haven't achieved nothing, we've prevented numberbase from being set
> to a value we can't handle.

That really depends on what the rest of the code does with  numberbase .
Perhaps there are parts of the code which can't handle all values from
2 to 16.

> But have we really done anything 
> significant as far as addressing the problems of global variables
> goes?

Since I don't know what the problems of global variables are supposed
to be , I can't answer this.

[toc] | [prev] | [next] | [standalone]


#151131

FromDavid Brown <david.brown@hesbynett.no>
Date2020-04-06 13:46 +0200
Message-ID<r6f4pv$d3c$1@dont-email.me>
In reply to#151123
On 06/04/2020 12:21, Malcolm McLean wrote:
> On Monday, 6 April 2020 11:16:57 UTC+1, Spiros Bousbouras  wrote:
>> On Mon, 6 Apr 2020 03:03:02 -0700 (PDT)
>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
>>> On Monday, 6 April 2020 10:25:24 UTC+1, Spiros Bousbouras  wrote:
>>>>
>>>>> I would say without /static/ keyword.
>>>>
>>>> Seems like a reasonable definition to me and the kind of thing I would
>>>> understand intuitively as a global variable.
>>>>
>>> Say we've got this
>>>
>>> static int numberbase = 10;
>>>
>>> void setnumberbase(int base)
>>> {
>>>     if(base < 2)
>>>       numberbase = 2;
>>>     else if(base > 16)
>>>       numberbase = 16;
>>>     else
>>>       numberbase = base;
>>> }
>>>
>>> int getnumberbase(void)
>>> {
>>>     return numberbase;
>>> }
>>>
>>> Our variable is protected - it can't go out of range. But it's
>>> globally visible. Anyone who includes our header can set the
>>> numberbase for the whole program.
>>
>> Function definitions should not go into headers. So you would put
>> in a header
>>
>> void setnumberbase(int) ;
>> int getnumberbase(void) ;
>>
>> and  #include  this in files which need to access  numberbase .So
>> what's the problem , what point were you trying to make ?
>>
> We've got rid a global variable with external linkage. And we
> haven't achieved nothing, we've prevented numberbase from being set
> to a value we can't handle. But have we really done anything
> significant as far as addressing the problems of global variables
> goes?
> 

Yes, that is what I meant by saying "knee-jerk alternatives are often 
equally bad".  Any part of the code can change "numberbase" as easily as 
if it were a global variable (using the above definition for that term).

Whether or not this is a problem depends on the rest of the code.  If 
the declaration for "setnumberbase" comes with a comment "Don't use this 
except at the beginning of main()", and all programmers have followed 
that rule, then there is no problem.  Equally, there would be no problem 
if there had been a globally visible "int numberbase" with the comment 
"Set this once at the beginning of main(), with a value between 2 and 16".

So the "solution" to "global variable problems" depends entirely on the 
problems you see.

[toc] | [prev] | [next] | [standalone]


#151148

Fromscott@slp53.sl.home (Scott Lurndal)
Date2020-04-06 18:05 +0000
Message-ID<FbKiG.895916$5o5.463453@fx41.iad>
In reply to#151048
Mehdi Amini <atorrses@gmail.com> writes:
>Hi,
>
>What are the ways to avoid global variables in C  ; This is one way that
>I know:
>
>// Code:
>#include <stdio.h>
>#include <stdlib.h>
>
>int gv=0;
>
>int main(void)
>{
>     gv++;
>     printf("gv is %d\n",gv);
>
>     return EXIT_SUCCESS;
>}
>
>// Code:
>#include <stdio.h>
>#include <stdlib.h>
>
>int func(void);
>
>int main(void)
>{
>     int v=func();
>     printf("v is %d\n",v);
>
>     return EXIT_SUCCESS;
>}
>
>int func(void)
>{
>     static int sv=0;

sv, in this context, is still global, albeit in a private namespace.

[toc] | [prev] | [next] | [standalone]


#151153

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-04-06 20:37 +0100
Message-ID<87sghg9zi0.fsf@bsb.me.uk>
In reply to#151148
scott@slp53.sl.home (Scott Lurndal) writes:

> Mehdi Amini <atorrses@gmail.com> writes:
<cut>
>>int func(void)
>>{
>>     static int sv=0;
>
> sv, in this context, is still global, albeit in a private namespace.

Or in C terms, sv still has static storage duration, albeit with a
block scope identifier with no linkage.  (Yours is less of a mouthful,
but not as precise.)

-- 
Ben.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web