Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > global/static variables & loops

Reply
Thread Tools

global/static variables & loops

 
 
BartC
Guest
Posts: n/a
 
      02-15-2012


"Eric Sosman" <(E-Mail Removed)> wrote in message
news:jhf24h$kmr$(E-Mail Removed)...
> On 2/14/2012 2:56 PM, Rob Janecek wrote:


>> In some coding advice I saw on the internet, we are urged to "avoid
>> referring to global or static variables inside the tightest loops".


> That said, I'd also point out that "avoid" is not absolute;
> perhaps "prefer not" would capture the notion more aptly. For
> example, it's hard to argue against the global variable in
>
> printf ("Hello, world!\n");


That's not likely to be part of a 'tight' loop though. The advice seems to
be more encouraging the use of globals than otherwise.

>> I also read (elsewhere) that access time to global/static and
>> automatic variables can be different on different hardware


> No specific usage X can always be counted on to be faster than
> some other usage Y.


No. But there might be the need to push an extra parameter for millions of
function calls, when all they want to do is access the same file-scope (or
project-scope) variable, which can be done for nothing.

And if they all need write-access to that variable, an extra level of
indirection is needed; avoiding a global access can well introduce overheads
that cannot be offset by potential optimisations. And that's just one
global...

--
Bartc

 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-15-2012
On 2012-02-15, BartC <(E-Mail Removed)> wrote:
>
>
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:jhf24h$kmr$(E-Mail Removed)...
>> On 2/14/2012 2:56 PM, Rob Janecek wrote:

>
>>> In some coding advice I saw on the internet, we are urged to "avoid
>>> referring to global or static variables inside the tightest loops".

>
>> That said, I'd also point out that "avoid" is not absolute;
>> perhaps "prefer not" would capture the notion more aptly. For
>> example, it's hard to argue against the global variable in
>>
>> printf ("Hello, world!\n");

>
> That's not likely to be part of a 'tight' loop though. The advice seems to
> be more encouraging the use of globals than otherwise.


Machine registers are globals, and they are used in tight loops.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      02-15-2012


"Kaz Kylheku" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2012-02-15, BartC <(E-Mail Removed)> wrote:
>> "Eric Sosman" <(E-Mail Removed)> wrote in message
>> news:jhf24h$kmr$(E-Mail Removed)...
>>> On 2/14/2012 2:56 PM, Rob Janecek wrote:

>>
>>>> In some coding advice I saw on the internet, we are urged to "avoid
>>>> referring to global or static variables inside the tightest loops".

>>
>>> That said, I'd also point out that "avoid" is not absolute;
>>> perhaps "prefer not" would capture the notion more aptly. For
>>> example, it's hard to argue against the global variable in
>>>
>>> printf ("Hello, world!\n");

>>
>> That's not likely to be part of a 'tight' loop though. The advice seems
>> to
>> be more encouraging the use of globals than otherwise.

>
> Machine registers are globals, and they are used in tight loops.


For those used as accumulators and scratchpad registers, then they don't
really correspond to the way actual global variables are used.

For those spare registers dedicated to particular uses by an application,
that just reinforces my point that access to globals can be streamlined
across different functions; they're always there for instant access.
(Although I'd imagine that in C, such uses are mainly for local variables
not globals.)

--
Bartc

 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      02-15-2012
On 2012-02-15 10:23, Nick Keighley wrote:
> Oh, and why does your progranm have global variables in the first
> place?


To handle a global state maybe.


August
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-15-2012
On Feb 15, 11:42*am, Kaz Kylheku <(E-Mail Removed)> wrote:
>
> Machine registers are globals, and they are used in tight loops.
>

Global and local are language concepts. You could certainly produce a
language in which registers are globals, and in fact some dialects of
C do that. You can refer to a register directly by name. But in most
languages registers are not available as globals.

--
Malcolm's website. Free games, programming material, and even some
poems.
http://www.malcolmmclean.site11.com/www



 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-15-2012
On Feb 15, 1:03*pm, August Karlstrom <(E-Mail Removed)> wrote:
> On 2012-02-15 10:23, Nick Keighley wrote:
>
> > Oh, and why does your progranm have global variables in the first
> > place?

>
> To handle a global state maybe.
>

A lot of programs work on a model / view paradigm. The model consists
of the data which the user is wanting to manipulate. The user
interface then provides various ways of seeing that data and issuing
instructions to manipulate it.

It often makes sense to have the model as a global.
--
MiniBasic - how to write a script interpreter
http://www.malcolmmclean.site11.com/www
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-15-2012
On 02/15/2012 08:20 AM, Malcolm McLean wrote:
> On Feb 15, 1:03�pm, August Karlstrom <(E-Mail Removed)> wrote:
>> On 2012-02-15 10:23, Nick Keighley wrote:
>>
>>> Oh, and why does your progranm have global variables in the first
>>> place?

>>
>> To handle a global state maybe.
>>

> A lot of programs work on a model / view paradigm. The model consists
> of the data which the user is wanting to manipulate. The user
> interface then provides various ways of seeing that data and issuing
> instructions to manipulate it.
>
> It often makes sense to have the model as a global.


Only if it never makes sense to have more than one instance of the model
manipulated by the same program. I've often written programs which only
manipulated a single model; but most, if not all, of those models were
things that there could have been two or more of in a single program.
When that's the case, it makes more sense to pass the model around
though pointer arguments.
--
James Kuyper
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      02-15-2012
On 2012-02-15 02:51, Stefan Ram wrote:
> The funny thing is that it's hard to explain this in
> beginner's classes, because there is one type of program
> where global variables don't hurt: very small programs.


I think it is more a question of static vs. non-static global variables.
As I see it, the only place for non-static global variables is in the
main translation unit (although it won't hurt to make them static there
too).


August
 
Reply With Quote
 
Anders Wegge Keller
Guest
Posts: n/a
 
      02-15-2012
Nick Keighley <(E-Mail Removed)> writes:

> Oh, and why does your progranm have global variables in the first
> place?


Can you give an example of a non-trivial program, that has no global
state in any way?

--
/Wegge

Leder efter redundant peering af dk.*,linux.debian.*
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-15-2012
On 02/15/2012 09:55 AM, Anders Wegge Keller wrote:
> Nick Keighley <(E-Mail Removed)> writes:
>
>> Oh, and why does your progranm have global variables in the first
>> place?

>
> Can you give an example of a non-trivial program, that has no global
> state in any way?


Most of the programs I'm responsible for were originally written by
other people, and they usually made a lot of use of global variables at
the time I became responsible for them. However, I've slowly been
removing those variables, and the only difficulty with doing so that
I've ever had was finding adequate justification for bothering to spend
the time needed to remove them.

The code I wrote entirely myself almost never uses globals, but I don't
know whether it would count as non-trivial for your purposes. However,
almost all of my code has to be linked to various libraries written by
other people, and those libraries often use globals, such as errno in
<errno.h>. errno isn't required to be a global, but it often is, so I
guess you're right - it all has some global state, even if there's no
real reason why global state is needed.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
For Loops and Variables Jason Cavett Java 18 04-10-2007 02:38 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
float variables in loops vijay C Programming 8 05-05-2005 08:40 AM
declaring variables inside for loops Grey Plastic C++ 3 08-10-2004 10:40 AM



Advertisments