CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Are these static variables local or global ?

    I came across the following code today and I was a bit surprised that it worked:-
    Code:
    std::string func_A ()
    {
          static std::string x;
    
          if (!x.empty())
                return x;
          else
          {
                x = "Hello";
                return x;
          }
    }
    
    std::string func_B ()
    {
          static std::string x;
    
          if (!x.empty())
                return x;
          else
          {
                x = "Goodbye";
                return x;
          }
    }
    I've simplified things slightly - but the basic point is that both functions are in the same source file and they both have a static std::string called 'x'. Being static, I guess they aren't (strictly) local variables. So how does the compiler know that they're different entities? Does it encode their signatures using the function name or something like that? If I call each function separately I do seem to get the correct string...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Are these static variables local or global ?

    Both
    Code:
       static std::string x;
    have their local scopes inside the functions where they are defined.
    So they are different variables.
    Victor Nijegorodov

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Are these static variables local or global ?

    Ah, of course! Thanks Victor
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Are these static variables local or global ?

    You will not have any collisions unless the variables have same name and are declared outside any class, function or namespace.

    Keep in mind that such a function-declared static variable can be very dangerous in a multithreading scenario. The issue arises because the first thread that accesses the function may need to initialize the static variable's default value. Multiple threads attempting to do that concurrently may cause a crash.
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Are these static variables local or global ?

    There's a few things going on here.
    1) they have local scope. I.e. the variables are only accessible in the compound statement they are defined in.
    2) they are constructed only once (because they're static). Construction happens when the compound statement is first executed.
    3) they're (obviously) only destroyed once. Which happend when the program terminates (after main() finishes), typically before global scope variables are destroyed, although there are no guarantees here. Destruction will only happen if the object got constructed of course.


    Item 3) may be important when this code is part of a DLL. For a dll, this means the destructor will be executed as part of the (real) DLL entrypoint (which calls DllMain()) for process detach.
    There are restrictions to what you can safely do in DllMain, so that extends to constructors and destructors of global scope objects and destructors for local scope objects. (it may apply to local scope constructors as well if the function is called as part of DllMain).

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Are these static variables local or global ?

    Quote Originally Posted by zerver View Post
    You will not have any collisions unless the variables have same name and are declared outside any class, function or namespace.
    Unless they are defined as static global variables in different compilation units. The keyword static has a different meaning when applied to global variables, than when applied to class/local variables (and still different when applied to member functions).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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