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


Groups > comp.lang.c > #17387

Re: global/static variables & loops

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: global/static variables & loops
Date 2012-02-15 15:00 -0500
Organization A noiseless patient Spider
Message-ID <jhh2sd$kt1$1@dont-email.me> (permalink)
References (3 earlier) <87k43o9mdn.fsf@huddi.jernurt.dk> <cd40f70d-efef-479d-b6fe-b468140a7763@t2g2000yqk.googlegroups.com> <87d39g9fgm.fsf@huddi.jernurt.dk> <jhgt9t$hkn$1@dont-email.me> <878vk3ao7m.fsf@huddi.jernurt.dk>

Show all headers | View raw


On 02/15/2012 02:30 PM, Anders Wegge Keller wrote:
> James Kuyper <jameskuyper@verizon.net> writes:
> 
>> On 02/15/2012 12:25 PM, Anders Wegge Keller wrote:
> 
>>>  That is global state in my book.
>  
>> If that counts as global state, then storing global state in global
>> variables is clearly not necessary.
> 
>  No, but when the effect is the same anyway, why add another level of
> complexity?

Because the effect is not the same - the visibility is quite different.

...
>> rather than the well-justified judgment that it actually is.
> 
>  If it quacks like a duck, walks like a duck and swims like a duck, it
> probably is a duck, no matter how you dress it up. 

Well, that works both ways - labeling it a prejudice doesn't make it
cease to be a well-justified judgment.

>> The key problem with global variables is visibility; by being
>> visible everywhere, they can be affected by anything, which can make
>> it difficult to determine what part of the program caused those
>> variables to have their current value, or what part had it's
>> behavior affected by the value.
> 
>  Scratch the getter/setter pattern, and the global struct pointer
> that's passed down the call tree then. *OR* prototype the darn things
> as const outside the translation unit that actually has to modify
> them, and see where the compiler complains. (And remember to remove
> the const prototype afterwards, unless you want to see some
> spectacular failures :)

I'm not sure what any of that word salad has to do with what I was
talking about. Can you re-write it to make the relevance clearer?

>> Variables which are local to main() and are passed by a
>> pointer to subroutines need not be passed to all subroutines, but only
>> the ones that actually need to have access to those variables, and that
>> could be a different set of subroutines for different variables. 
> 
>  Unless you have a very limited state space, that will lead to some
> very convoluted and error-prone code. 

Not in my experience.

>> Making information available to a subroutine only on a "need to
>> know" basis is good software design. It can be overdone, but that's
>> seldom a real-world problem.
> 
>  You should try to do real-time machine control with 10-15
> co-concurrent tasks running on a 80186 platform, where each individual
> thread has a stack in the range 128 to 256 bytes, and avoid overdoing
> need-to-know :) I'm in the 0.1%, where overdoing things are a
> real-world problem.

Limited stack space is something I've little recent experience with, but
I agree that it's an excellent reason to avoid using the stack; in
extreme cases, even passing pointers on the stack becomes problematic -
but you should rarely need to go all the way to global visibility to
avoid that problem. You have a greater need to use objects with static
storage duration than I do, but you don't have to make every single one
of them globally visible. I've a number of such objects in my own code.
Most of them have block scope, a few have file scope - but none has
external linkage.
-- 
James Kuyper

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

global/static variables & loops Rob Janecek <robs@invalid.com> - 2012-02-14 19:56 +0000
  Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-14 12:42 -0800
  Re: global/static variables & loops Stephen Sprunk <stephen@sprunk.org> - 2012-02-14 15:01 -0600
  Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-14 20:35 -0500
    Re: global/static variables & loops Philip Lantz <prl@canterey.us> - 2012-02-14 21:18 -0800
    Re: global/static variables & loops pete <pfiland@mindspring.com> - 2012-02-15 00:53 -0500
      Re: global/static variables & loops Jonathan Leffler <jleffler@earthlink.net> - 2012-02-18 21:15 -0800
        Re: global/static variables & loops Kaz Kylheku <kaz@kylheku.com> - 2012-02-19 06:05 +0000
          Re: global/static variables & loops Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-22 03:15 +0200
        Re: global/static variables & loops Shao Miller <sha0.miller@gmail.com> - 2012-02-19 04:14 -0500
          Re: global/static variables & loops Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-19 12:32 +0000
            Re: Array 'typedef's and exclusive use of '->' versus '.' Shao Miller <sha0.miller@gmail.com> - 2012-02-19 17:00 -0500
              Re: Array 'typedef's and exclusive use of '->' versus '.' Keith Thompson <kst-u@mib.org> - 2012-02-19 16:43 -0800
                Re: Array 'typedef's and exclusive use of '->' versus '.' Shao Miller <sha0.miller@gmail.com> - 2012-02-19 20:32 -0500
              Re: Array 'typedef's and exclusive use of '->' versus '.' Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-22 03:27 +0200
                Re: Array 'typedef's and exclusive use of '->' versus '.' Shao Miller <sha0.miller@gmail.com> - 2012-02-25 02:19 -0500
                Re: Array 'typedef's and exclusive use of '->' versus '.' Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-25 10:14 +0200
    Re: global/static variables & loops "BartC" <bc@freeuk.com> - 2012-02-15 10:47 +0000
      Re: global/static variables & loops Kaz Kylheku <kaz@kylheku.com> - 2012-02-15 11:42 +0000
        Re: global/static variables & loops "BartC" <bc@freeuk.com> - 2012-02-15 12:54 +0000
        Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 05:17 -0800
      Re: global/static variables & loops Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-22 03:28 +0200
    Re: global/static variables & loops August Karlstrom <fusionfile@gmail.com> - 2012-02-15 15:19 +0100
    Re: global/static variables & loops Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-02-22 03:12 +0200
  Re: global/static variables & loops Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-15 01:23 -0800
    Re: global/static variables & loops August Karlstrom <fusionfile@gmail.com> - 2012-02-15 14:03 +0100
      Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 05:20 -0800
        Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 08:41 -0500
          Re: global/static variables & loops Kaz Kylheku <kaz@kylheku.com> - 2012-02-16 00:20 +0000
            Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-15 21:02 -0500
              Re: global/static variables & loops "BartC" <bc@freeuk.com> - 2012-02-16 10:03 +0000
                Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-16 07:58 -0500
              Re: global/static variables & loops Markus Wichmann <nullplan@gmx.net> - 2012-02-19 20:44 +0100
                Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-19 15:37 -0500
                Re: global/static variables & loops Kaz Kylheku <kaz@kylheku.com> - 2012-02-22 05:01 +0000
                Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-22 08:12 -0500
    Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 15:55 +0100
      Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 11:05 -0500
      Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 09:13 -0800
        Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 18:25 +0100
          Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 13:25 -0500
            Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 10:51 -0800
              Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 14:45 -0500
              Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 20:47 +0100
                Re: global/static variables & loops Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-16 04:45 -0800
                Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-16 14:25 +0100
                Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 08:10 -0800
                Re: global/static variables & loops Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-17 00:59 -0800
            Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 20:30 +0100
              Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 15:00 -0500
                Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 21:30 +0100
                Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 16:00 -0500
                Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 23:10 +0100
                Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-15 15:50 -0800
                Re: global/static variables & loops James Kuyper <jameskuyper@verizon.net> - 2012-02-15 19:50 -0500
                Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-16 14:36 +0100
            Re: global/static variables & loops Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-16 03:29 -0800
      Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-15 23:37 +0100
      Re: global/static variables & loops Kaz Kylheku <kaz@kylheku.com> - 2012-02-16 00:23 +0000
        Re: global/static variables & loops Anders Wegge Keller <wegge@wegge.dk> - 2012-02-16 01:29 +0100
        Re: global/static variables & loops Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-15 21:20 -0500
      Re: global/static variables & loops Shao Miller <sha0.miller@gmail.com> - 2012-02-15 22:24 -0500
        Re: global/static variables & loops Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 01:08 -0800
      Re: global/static variables & loops Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-16 03:17 -0800

csiph-web