469,963 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Static variable vs. global variable

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
Jun 27 '08 #1
2 2003
Read the following about storage duration, scope, and linkage. (it is
for C but applies to C++ too.)

http://www.cnblogs.com/WarrenTang/ar...0/1129991.html

drmario wrote:
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

Jun 27 '08 #2
On Apr 14, 11:59*pm, "drmario" <drma...@cox.netwrote:
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

Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marcin Vorbrodt | last post: by
9 posts views Thread by Bryan Parkoff | last post: by
3 posts views Thread by Datta Patil | last post: by
9 posts views Thread by AnandRaj | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
14 posts views Thread by Jess | last post: by
10 posts views Thread by Pramod | last post: by
14 posts views Thread by cs1975 | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.