What's so special about static variables? - Programmer

This is a discussion on What's so special about static variables? - Programmer ; MFC documentation says that a static variable is allocated when the program begins & is de-allocated when the programs ends. How is this any different than global variables? - Tony...

+ Reply to Thread
Results 1 to 3 of 3

Thread: What's so special about static variables?

  1. What's so special about static variables?

    MFC documentation says that a static variable is allocated when the
    program begins & is de-allocated when the programs ends. How is this
    any different than global variables? - Tony

  2. Re: What's so special about static variables?

    "TonyVarden" wrote in message
    news:739e44f7.0401111727.5518bee6@posting.google.c om...
    > MFC documentation says that a static variable is allocated when the
    > program begins & is de-allocated when the programs ends. How is this
    > any different than global variables? - Tony


    Global is a scope, static is a storage class.

    All globals are static (effectively - see below) but not all statics are
    global.

    Global variables are visible (and modifiable) from anywhere in your program.
    For this reason, they are usually bad programming practice and should
    generally be avoided but there are some rare situations where they are
    useful. Local variables can only be seen & modified within the scope they
    are declared in.

    Automatic variables (the opposite of static) are initialized every time they
    come into scope and are not guaranteed to keep their value once they go out
    of scope. Static variables are only initialized at the beginning of the
    program and do keep their value when they go out of scope.

    Globals only come into scope once - at the beginning of the program - and
    stay in scope until the end of the program so it doesn't matter if they are
    considered dynamic OR static. The end result is the same.

    --
    Terry R.
    My return address is munged



  3. Re: What's so special about static variables?

    Well, that depends on what you mean.
    Here are the differences.

    1. A class static variable is "visible" only in this class;
    2. A file static variable is visible only in this file;
    3. A function static variable is visible only in this function.

    In general, a static variable is a global variable with
    scope limitations.

    "TonyVarden" ???????/???????? ? ???????? ?????????:
    news:739e44f7.0401111727.5518bee6@posting.google.c om...
    > MFC documentation says that a static variable is allocated when the
    > program begins & is de-allocated when the programs ends. How is this
    > any different than global variables? - Tony




+ Reply to Thread