CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    what is the difference b/w a normal global variable and a global static variable

    Hi,
    Can anyone explain me about the difference b/w a normal global variable and a global static variable.

    ex:
    if i have code like

    int i;
    static int j;
    both are global variable, what is the difference b/w these two variables.

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: what is the difference b/w a normal global variable and a global static variable

    Global variable defined with static keyword has a scope limited to translation unit it is defined in. This variable is not "totally" global (possible to use in other source files), it is visible and usable only in this source file (translation unit, to be exact).
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Sep 2008
    Posts
    48

    Re: what is the difference b/w a normal global variable and a global static variable

    Upto my knowledge, even the normal global variable's scope also get expire at the end of the program. If we want to use a variable in more than 1 source file, we may have to use 'export' keyword.

    This is what my understanding about global variables, pls clarify me on this.

    Thanks
    Kiran.

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: what is the difference b/w a normal global variable and a global static variable

    Okay, so maybe a little example:

    File a.cpp:
    Code:
    int global = 0;   //define global variable
    static int static_global = 42;   //define static global variable
    
    void fun1() {
       
       //function within the dame translation unit may access both global variables
       global = 3;
       static_global = 13;
    }
    File b.cpp:
    Code:
    //declare variables
    extern int global;
    extern int static_global;
    
    void fun2() {
       
       //function within another translation unit may not access
       //static global variables defined elsewhere
       global = 16;  //OK
       static_global = 102;  //Linker error, undefined in this translation unit
    }

    export keywoord does not apply to variables.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Sep 2008
    Posts
    48

    Re: what is the difference b/w a normal global variable and a global static variable

    That's gr8, your example is very much clear.

    Apart from this scope functionality, is there anything else difference is there ?, like memory allocation or initialization or accessing the variables and so .

    Thanks

  6. #6
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: what is the difference b/w a normal global variable and a global static variable

    No, there is no difference. Both those cases are global variables, they behave in exactly the same way.
    Generally, you should avoid using global variables. They introduce a place for various pitfalls, often leading to very long, depressing and furious debugging sessions.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured