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. Advertisements

  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. Advertisements

  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. Advertisements

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. Marc Missire
    Replies:
    3
    Views:
    4,038
    Marc Missire
    Jul 25, 2005
  2. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,678
    Denis Perelyubskiy
    Sep 5, 2003
  3. Bryan Parkoff
    Replies:
    2
    Views:
    6,902
  4. Lu
    Replies:
    2
    Views:
    909
    Mark McIntyre
    Jul 8, 2003
  5. Replies:
    1
    Views:
    511
    Michael Ekstrand
    Aug 21, 2005
  6. gomathy

    Global access of static variable

    gomathy, Oct 15, 2004, in forum: C Programming
    Replies:
    2
    Views:
    376
    Barry Schwarz
    Oct 15, 2004
  7. jubelbrus
    Replies:
    5
    Views:
    834
    JohnQ
    Jul 20, 2007
  8. Mark
    Replies:
    2
    Views:
    556
    Jim Langston
    Oct 16, 2007
Loading...