Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifetime of a static variable in a C++ function?

If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called?

void foo()  {      static string plonk = "When will I die?"; } 
like image 663
Motti Avatar asked Oct 29 '08 12:10

Motti


People also ask

What is the life time of variable?

The lifetime of a variable or object is the time period in which the variable/object has valid memory. Lifetime is also called "allocation method" or "storage duration."

What is scope and lifetime of static variable in C?

Scope Of A Variable in C. Lifetime Of A Variable in C. Scope of a variable determines the area or a region of code where a variable is available to use. Lifetime of a variable is defined by the time for which a variable occupies some valid space in the system's memory. Scope determines the life of a variable.

What is the lifetime of a variable in C?

The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between allocation and deallocation of memory). In C language, a variable can have automatic, static or dynamic lifetime. Automatic − A variable with automatic lifetime are created.

What is the value of static variable in C?

Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.


1 Answers

The lifetime of function static variables begins the first time[0] the program flow encounters the declaration and it ends at program termination. This means that the run-time must perform some book keeping in order to destruct it only if it was actually constructed.

Additionally, since the standard says that the destructors of static objects must run in the reverse order of the completion of their construction[1], and the order of construction may depend on the specific program run, the order of construction must be taken into account.

Example

struct emitter {     string str;     emitter(const string& s) : str(s) { cout << "Created " << str << endl; }     ~emitter() { cout << "Destroyed " << str << endl; } };  void foo(bool skip_first)  {     if (!skip_first)         static emitter a("in if");     static emitter b("in foo"); }  int main(int argc, char*[]) {     foo(argc != 2);     if (argc == 3)         foo(false); } 

Output:

C:>sample.exe
Created in foo
Destroyed in foo

C:>sample.exe 1
Created in if
Created in foo
Destroyed in foo
Destroyed in if

C:>sample.exe 1 2
Created in foo
Created in if
Destroyed in if
Destroyed in foo

[0] Since C++98[2] has no reference to multiple threads how this will be behave in a multi-threaded environment is unspecified, and can be problematic as Roddy mentions.

[1] C++98 section 3.6.3.1 [basic.start.term]

[2] In C++11 statics are initialized in a thread safe way, this is also known as Magic Statics.

like image 184
Motti Avatar answered Oct 11 '22 04:10

Motti