Static variable vs. global variable

D

drmario

Using Microsoft VC++2008 Windows XP

I don't understand, for all the reading I've just done on the subject, what
the difference there is. I mean if I declare a variable with global (file)
scope, I can get to it from anywhere in my program. From what I understand,
if I declare a static (and I think I have use external static?) variable
inside a function, the only way that differs from declaring it globally is
that it won't be instantiated until the program execution reaches it. I
can't see how that difference would possibly be useful, so what gives?

cheers,
Mario
 
I

Ian Collins

drmario said:
Using Microsoft VC++2008 Windows XP

I don't understand, for all the reading I've just done on the subject, what
the difference there is.

Difference between what?
I mean if I declare a variable with global (file)
scope, I can get to it from anywhere in my program. From what I understand,
if I declare a static (and I think I have use external static?) variable
inside a function, the only way that differs from declaring it globally is
that it won't be instantiated until the program execution reaches it. I
can't see how that difference would possibly be useful, so what gives?
"external static" is an oxymoron.

There is no fixed order of initialisation of file scope (or class )
static variables. A function scope static variable has a known point of
initialisation.
 
G

Greg Herlihy

Using Microsoft VC++2008 Windows XP

I don't understand, for all the reading I've just done on the subject, what
the difference there is.  I mean if I declare a variable with global (file)
scope, I can get to it from anywhere in my program.  From what I understand,
if I declare a static (and I think I have use external static?) variable
inside a function, the only way that differs from declaring it globally is
that it won't be instantiated until the program execution reaches it.  I
can't see how that difference would possibly be useful, so what gives?

Locally-scoped static variables provide a way for a C++ program to
defer relatively expensive initialization costs - until such time that
the service being initialized is actually needed. Otherwise, with
ordinary globals, the program would initialize everything at startup -
and make the user wait, unnecessarily.

Greg
 
J

James Kanze

Using Microsoft VC++2008 Windows XP
I don't understand, for all the reading I've just done on the
subject, what the difference there is. I mean if I declare a
variable with global (file) scope, I can get to it from
anywhere in my program. From what I understand, if I declare
a static (and I think I have use external static?) variable
inside a function, the only way that differs from declaring it
globally is that it won't be instantiated until the program
execution reaches it. I can't see how that difference would
possibly be useful, so what gives?

There are two important differences. The first is that if you
declare the variable local, it can't be seen outside of the
function, so you avoid poluting any wider namespaces. The
second is, as you say, that it will be initialized the first
time control flow reaches the declaration---this is often used
to manage order of initialization issues, for example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top