-
March 10th, 2014, 04:45 AM
#1
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
-
March 10th, 2014, 05:29 AM
#2
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
-
March 10th, 2014, 05:51 AM
#3
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
-
March 10th, 2014, 07:27 AM
#4
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
-
March 10th, 2014, 07:46 AM
#5
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).
-
March 11th, 2014, 07:05 AM
#6
Re: Are these static variables local or global ?
Originally Posted by zerver
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|