Reputation: 3658
I have a global object which is declared before main function and a static object inside main.
A obj1
is also called static; why is that? class A { ... };
A obj1;
int main()
{
static A obj2;
}
Upvotes: 2
Views: 504
Reputation: 208343
My first doubt is how precise the question is. If static initailization is used with the technical meaning in the standard, it represents zero-initialization and initialization of a POD type with constant expressions for objects with storage duration, compared with dynamic initialization where the initialization of the object with static storage duration is initialized somehow else.
For an illustrative example:
// at namespace level
int f();
int a; // 1 static: zero initialization
int b = 10; // 2 static: initialization from constant expression
int c = f(); // 3 static (zero initialization)
// 5 followed by dynamic (result of call to f())
int d = 20; // 4 static: initialization
int f() { return d; }
int main() {}
The number indicates the order of execution of each initialization step. The reason why the compiler considers both static and dynamic initialization is that it sets an order on the execution of the initialization. static initialization is executed before dynamic initialization, and all statically initialized objects are guaranteed to have their value before any dynamic initialization starts. That is, the standard guarantees that even if d
appears after c
in the previous program, the value of c
is guaranteed to be 20
.
For objects that require dynamic initialization, they are (conceptually) zero initialized during static initailization, so during the lifetime of c
, it is first set to 0, and later reset to f()
(or 20 in this program). The conceptually is due to the fact that if the compiler can infer the final value that the variable will get, it can optimize the dynamic initialization away and just perform plain static initialization with that final value (in the above code, the compiler could detect that c
is going to be dynamically initialized to 20, and decide to transform it into static initialization like int c = 20;
in a conforming implementation. In that case steps 3 and 5 in the code above would be merged into a single step 3.
In the case of a local variable with static storage duration, the standard does not use the terms static/dynamic initialization, but the descriptions require the same behavior from the program. Local variables with static duration are zero-initialized or POD initialized with constant expressions when (or before) the block is entered for the first time (as with static initialization), while for the rest of the initializations are performed the first time that control passes over through it's declaration.
Upvotes: 3
Reputation: 32398
Both of these variables are static:
obj1
is a non-local static variable which will be initialized when the program starts.
obj2
is a local static variable which will be initialised when the function is first called.
Scott Meyers in his book "Effective C++" recommends accessing local static variables through functions as opposed to using non-local static variables. Doing so avoids the so called static initialization order problem where one variable can reference another which may not have been initialized yet because of the arbitrary order in which initialization occurs.
Upvotes: 1
Reputation: 5920
Focusing on C++ only, you also need to consider initialization of static members. It concerns static members of a class. For example, when you have:
class A {
// declaration of i as a static member of class A
static int i;
};
// initialization of static member outside class A
int A::i = 42;
Upvotes: 1
Reputation: 612964
obj1
has static storage. It will be initialized when the program starts.
obj2
also has static storage because you said so. It will be initialized when main()
executes the first time.
Upvotes: 4