Thread: static global

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35

    static global

    hi iam having small doubt.

    what is the difference between static and static global variable.

    could some body clarify.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    static local var is visible only in the function where it is declared
    static "global" var is visible in any function of the current source file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35
    thank you for reply

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    A bit further expanation. A static variable defined in a function is different
    from a normal variable defined in a function because a static variable will not be
    freed at the end of the function. This allows its reference or pointer to be returned from
    the function and it will have the same value the next time you call the function.

    Code:
    int& something()
    {
      static int myVar1; //will not be freed at end of function
      int myVar2;  //will be freed at end of function so you cannot return its reference.
      
      printf("%i\n", myVar1); //The first time the function is called this may print junk. The next time it should print 32
      myVar1 = 32;
      myVar2 = 32;
      return myVar1;
    }
    In other words its like a global variable that can only be accessed inside the functions
    scope and through its return value.

    A global variable is defined outside of a function, class, structure, etc.

    Code:
    int myGlobal = 32;
    
    void something()
    {
      printf("%i\n", myGlobal); //prints 32
    }

    I dont think it matters if you put the word static in front of a global variable because
    it should always be static (static, as in it wont be freed until the end of the program).

    EDIT: As mentioned below, static does in fact affect global variables. It limits its scope to the C file it is defined in, meaning you can not use that variable in another C file, even if you use "extern".
    Last edited by 39ster; 01-14-2008 at 07:00 PM.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Is there a difference in where the two are stored in memory?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by cs32 View Post
    Is there a difference in where the two are stored in memory?
    in general - no, but it is upto compiler.

    returning pointer or ref to the static var could rise some problems in Multi-threading envirounment, so should be avoided
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 39ster View Post
    I dont think it matters if you put the word static in front of a global variable because
    it should always be static (static, as in it wont be freed until the end of the program).
    As mentioned, static before the global makes its linkage internal. That is, you can't access it from other .c files. If you don't add "static" before the global, then its linkage will be external and it can be accessed from other .c files.
    Note that the opposite applies to C++! You have to specifically declare a global variable as extern to make it external linkage.

    Quote Originally Posted by vart View Post
    returning pointer or ref to the static var...
    Not to mention, global variables!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    Note that the opposite applies to C++! You have to specifically declare a global variable as extern to make it external linkage.
    What are you referring to? Could you give an example?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I meant is,
    Code:
    int x = 5;
    External linkage in C. Can be accessed from multiple .c files.
    In C++, however, it's internal linkage so trying to access it from another .cpp file will yield a compile error and redefining it will yield a linking error.

    Code:
    static int x = 5;
    Internal linkage in C. Cannot be accessed from other .c files.
    I don't know what the effect is in C++, though. I don't know if it will cause external linkage in this case.

    Code:
    extern int x;
    If we add this in C++, THEN it's external linkage.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    But to get external linkage in C, don't you need to do extern in the other file you want to use it in?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I'm not a C programmer, but try this:
    Do something like:

    int x = 5;
    In one .c file.
    Then print the value in another .c file with extern:
    extern int x;
    It will work.

    Now try adding
    static int x = 5;
    and then
    extern int x;
    Oops, linking error.

    The only thing I know is that the same example works in C++ and causes an error as well
    Yeah, someone has explained the linkage before, but I don't think I'm an expert at it.
    Last edited by Elysia; 01-14-2008 at 04:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yeah exactly, so what's the difference?

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Other C files cannot access variables that have been defined as static. Only the C file that declared the variable can access it even if you use extern in the other C file -_-.

    Havnt tried it but it might allow you to create variables with the same name and definition in different C files.

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    'global' variables declared in seperate files have file scope only. Specifying them as static does nothing, as global variabels remain in scope until the application terminates. You can get away with reusing the same variable name in different files and it will have a different memory location for each copy, btu then you cannot use extern. Even a single extern will cause problems as your compiler will either randomly select which one to use, or toss an error about redefinition.

    Declaring a varible as static within a function can have ramifications for reentrant code, so be careful if multiple threads access functions with static variables. This can lead to useful or horrendous behavior depending on how they are used.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >Even a single extern will cause problems as your compiler will either randomly select which one to use, or toss an error about redefinition.
    no, extern prevents redefinition errors, and the one that doesn't have the extern will be the one that is used.

    Elysia:
    Code:
    C:\>type f1.c
    int x;
    extern int y;
    void func();
    int u;
    
    int main()
    {
    }
    
    
    C:\>type f2.c
    extern int x;
    
    static int y;
    
    void func()
    {
            u ++;
    
            return;
    }
    
    C:\>gcc f1.c f2.c -Wall -Werror -pedantic -std=c99
    f2.c: In function `func':
    f2.c:7: error: `u' undeclared (first use in this function)
    f2.c:7: error: (Each undeclared identifier is reported only once
    f2.c:7: error: for each function it appears in.)
    f2.c: At top level:
    f2.c:3: warning: 'y' defined but not used
    
    C:\>move f1.c f1.cc
    
    C:\>move f2.c f2.cc
    
    C:\>g++ f1.cc f2.cc -Wall -Werror -pedantic
    f2.cc: In function `void func()':
    f2.cc:7: error: `u' undeclared (first use this function)
    f2.cc:7: error: (Each undeclared identifier is reported only once for each funct
    ion it appears in.)
    f2.cc: At global scope:
    f2.cc:3: warning: 'y' defined but not used
    
    C:\>
    Am I missing something?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM