Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is static variable initialization implemented by the compiler?

Tags:

I'm curious about the underlying implementation of static variables within a function.

If I declare a static variable of a fundamental type (char, int, double, etc.), and give it an initial value, I imagine that the compiler simply sets the value of that variable at the very beginning of the program before main() is called:

void SomeFunction();  int main(int argCount, char ** argList) {     // at this point, the memory reserved for 'answer'     // already contains the value of 42     SomeFunction(); }  void SomeFunction() {     static int answer = 42; } 

However, if the static variable is an instance of a class:

class MyClass {     //... };  void SomeFunction();  int main(int argCount, char ** argList) {     SomeFunction(); }  void SomeFunction() {     static MyClass myVar; } 

I know that it will not be initialized until the first time that the function is called. Since the compiler has no way of knowing when the function will be called for the first time, how does it produce this behavior? Does it essentially introduce an if-block into the function body?

static bool initialized = 0; if (!initialized) {     // construct myVar     initialized = 1; } 
like image 382
e.James Avatar asked May 22 '09 15:05

e.James


People also ask

How are static variables initialized?

The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.

How is a static variable implemented in C?

The static variable is kept in the memory till the end of the program, whereas a normal variable is destroyed when a function is over. The static variable inside the function holds the value not only till the end of the function block but till the end of the entire program.

Are static variables initialized at compile time?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.

What is the order of static variable initialization across one program?

C++ guarantees that variables in a compilation unit (. cpp file) are initialised in order of declaration. For number of compilation units this rule works for each one separately (I mean static variables outside of classes). But, the order of initialization of variables, is undefined across different compilation units.


1 Answers

This question covered similar ground, but thread safety wasn't mentioned. For what it's worth, C++0x will make function static initialisation thread safe.

(see the C++0x FCD, 6.7/4 on function statics: "If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.")

One other thing that hasn't been mentioned is that function statics are destructed in reverse order of their construction, so the compiler maintains a list of destructors to call on shutdown (this may or may not be the same list that atexit uses).

like image 99
James Hopkin Avatar answered Oct 09 '22 23:10

James Hopkin