Static variable vs. global variable

Discussion in 'C++' started by drmario, Apr 15, 2008.

  1. drmario

    drmario Guest

    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
    drmario, Apr 15, 2008
    #1
    1. Advertising

  2. drmario

    Ian Collins Guest

    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.


    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.

    --
    Ian Collins.
    Ian Collins, Apr 15, 2008
    #2
    1. Advertising

  3. drmario

    Warren Tang Guest

    Read the following about storage duration, scope, and linkage. (it is
    for C but applies to C++ too.)

    http://www.cnblogs.com/WarrenTang/archive/2008/03/30/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
    >
    >
    Warren Tang, Apr 15, 2008
    #3
  4. drmario

    Greg Herlihy Guest

    On Apr 14, 11:59 pm, "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?


    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
    Greg Herlihy, Apr 15, 2008
    #4
  5. drmario

    James Kanze Guest

    On Apr 15, 8:59 am, "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?


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 15, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,415
    Denis Perelyubskiy
    Sep 5, 2003
  2. Bryan Parkoff
    Replies:
    2
    Views:
    6,496
  3. Replies:
    1
    Views:
    383
    Michael Ekstrand
    Aug 21, 2005
  4. jubelbrus
    Replies:
    5
    Views:
    613
    JohnQ
    Jul 20, 2007
  5. Mark
    Replies:
    2
    Views:
    419
    Jim Langston
    Oct 16, 2007
Loading...

Share This Page