When are static data initialized ?
I think:
It can be initialized by
constructor,
or
when it is declared,
or
outside of the class by
class A::member_d = 5; // member_d is static
others ?
Also, When do file scope static variables initialized and when do function scope static variables initizliaed?
I think they are initialized when they are declared.
thanks
Static members of a class are initialized at the point of definition. Const integral data types are an exception that can be initialized at the point of declaration. When such initialization is going to be executed is somewhat complicated (google for static initialization fiasco). According to the standard:
If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use of any function or variable defined in the same translation unit as the variable to be initialized.
Static Storage duration object initialization:
Note: Static member objects are initialized the same way as objects at file scope.
Objects at file scope are initialized at point of definition.
You can think of these as all being initialized before main() is called.
See details below.
Objects in function scope are initialized the first time execution passes over the definition.
ie Usually the first time the function is called.
The object is initialized by the initializer in the definition. if you do not provide an initializer then it will be zero-initialized.
int x1; // zero initialized.
int x2 = 5; // initialized with 5
A y1; // Default initialized:
// If A has a user defined constructor this is called.
// If A has a compiler generated constructor then
// it is value-initialized which means class objects have their
// default constructor called and POD objects are zero-initialized
A y2 = A(); // Default constructed.
A y3(5); // Constructor called with value 5.
Static members are exactly the same as objects as file scope.
class Z
{
static int x1; // declaration;
static int x2;
static A y1;
static A y2;
static A y3;
};
// definition
int Z::x1;
int Z::x2 = 5;
A Z::y1;
A Z::y2 = A();
A Z::y3(5);
Now the order that they are initialized is harder to define. The order is deliberately left vague to allow for compiler and linker situations that the committee could not foresee.
It is defined in:
3.6.2 Initialization of non-local variables
The main thing to note is:
Non-local variables with static storage duration are initialized as a consequence of program initiation.
So in most situations they will all be fully constructed before main is entered.
As noted by others. The compiler is allowed to delay initialization.
3.6.2 Paragraph 4
It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main.
This addition is mainly done to support dynamically loading libraries at runtime (which may by laded dynamically after main has started). But it does provide a simple guarantee that all static storage duration objects within a compilation until will be fully constructed before any objects or function in that compilation are used.
3.6.2 Paragraph 5
If the initialization is deferred to some point in time after the first statement of the initial function of the thread, it shall occur before the first odr-use (3.2) of any variable with thread storage duration defined in the same translation unit as the variable to be initialized.
Global variables are initialized at program start-up, before main() is invoked. Static objects that are local to a scope are initialized the first time execution passes over them.
Static class members are just global variables, so see above.
Destruction of global and static objects happens after main() returns.
(The implementation details of this are fairly intricate, since all the destructors have to be queued up for execution somewhere, and for local statics there needs to be a flag to indicate whether the object has already been instantiated.)
No, of course, the constructor can't initialize static data members. For const integral or enumeration types, you can initialize within the scope of the class definition. However, in general, you must initialize outside the class definition.
Related
I wonder if all constants in C++ have static duration, even though they are created inside a function other than main()?
For example
const int a = 3;
int main()
{
const int b = 4;
}
What is the difference between a and b?
Whether an object is const and whether it has static storage duration are unrelated. An object defined inside a function has automatic storage duration unless explicitly marked static or thread_local. A static data member of a class has static storage duration unless explicitly marked thread_local. An object defined at namespace scope has static storage duration unless explicitly marked thread_local.
No; this is clearest if you create a const object with a constructor and destructor, which will be called when the program enters and leaves the scope in which the automatic const object is declared. The const automatic variable can also have different values on each invocation of the function, based on the parameters or other runtime data, while a static const local variable could not.
A constexpr object, however, cannot have non-trivial constructor or destructor or be initialized to anything but constants known at compile time, so it could be implemented the same way as a static variable.
I encountered this C++ static initialization order related query at a code review recently.
I've a class with static member variable in a compilation unit
I've a static object of that class using a constructor in a different compilation unit
Here, I want to know if the static member variable is guaranteed to be initialized before the static object constructor is called?
MyClass.h:
typedef int (*MyFunc)(int);
class MyClass {
MyClass(MyFunc fptr) {
mFunc = fptr;
}
static MyFunc mFunc;
}
MyClass.cpp:
MyFunc MyClass::mFunc = nullptr;
MyDifferentClass.h:
MyDifferentClass {
public:
static int MyStaticFunc(int);
}
MyDifferentClass.cpp:
static MyClass myClassObj(MyDifferentClass::MyStaticFunc);
In the code, would mFunc be initialized to nullptr before myClassObj gets created? The reason for the query is that if the order is not guaranteed, then mFunc may get initialized again to nullptr.
In the code, would mFunc be initialized to nullptr before myClassObj gets created? The reason for the query is that if the order is not guaranteed, then mFunc may get initialized again to nullptr.
The answer to the question is "Yes".
Setting aside the issue of initialization of thread specific objects, initialization of non-local variables is carried out in the following order.
All variables are zero-initialized (order not specified). This is called zero initialization.
All variables that can be initialized using constant values are initialized. This is called constant initialization.
Those ( 1 and 2 above) are called static initialization.
After that, dynamic initialization is performed.
In your case, MyClass::mFunc is initialized using constant initialization while myClassObj is initialized using dynamic initialization. Hence, the former is guaranteed to be initialized first.
More on this topic can be found at https://timsong-cpp.github.io/cppwp/n3337/basic.start.init.
How should I work with exceptions throwed from constructors of local static objects? For example I have following code:
class A
{
public:
A() {throw runtime_error("Ooops");}
};
void foo()
{
static A a = A();
cout << "Continue" << endl;
}
int main(void)
{
try
{
foo();
}
catch(...)
{
}
foo(); // Prints continue
return 0;
}
As I understand, in the case of second calling foo method, object a is treated as fully constructed object, and constructor is not called. (More over, it seems like destructor of a due first exception throwing is not called)
If that's true, then that is a compiler bug.
(However, VTT claims that this code produces the expected result with Visual Studio 2015, so I recommend double-checking your results.)
Here's the standard-mandated behaviour:
[C++14: 6.7/4]: The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or
thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined. [..]
GCC 6.3.0 correctly attempts to reconstruct the A (and thus throws again).
More over, it seems like destructor of a due first exception throwing is not called
No, it won't be. You can't destroy an object that was never successfully created in the first place.
[C++14: 15.2/2]: An object of any storage duration whose initialization or destruction is terminated by an exception will have destructors executed for all of its fully constructed subobjects (excluding the variant members of a union-like class), that is, for subobjects for which the principal constructor (12.6.2) has completed execution and the destructor has not yet begun execution. Similarly, if the non-delegating constructor for an object has completed execution and a delegating constructor for that object exits with an exception, the object’s destructor will be invoked. If the object was allocated in a new-expression, the matching deallocation function (3.7.4.2, 5.3.4, 12.5), if any, is called to free the storage occupied by the object.
By the way, this doesn't fix the problem, but you should just write:
static A a;
The copy-initialisation from a temporary is pointless.
Each instance of static local variable also implicitly creates a global Boolean variable that will be set to true after static variable is constructed. If constructor throws than the next time method is called there will be another attempt to construct a static variable.
I have a global object which is declared before main function and a static object inside main.
Which one of these uses (or do both?) static initialization?
I've heard that A obj1 is also called static; why is that?
class A { ... };
A obj1;
int main()
{
static A obj2;
}
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.
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.
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;
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.
If I have a class called Test ::
class Test
{
static std::vector<int> staticVector;
};
when does staticVector get constructed and when does it get destructed ?
Is it with the instantiation of the first object of Test class, or just like regular static variables ?
Just to clarify, this question came to my mind after reading Concepts of Programming Languages (Sebesta Ch-5.4.3.1) and it says ::
Note that when the static modifier
appears in the declaration of a
variable in a class definition in C++,
Java and C#, it has nothing to do with
the lifetime of the variable. In that
context, it means the variable is a
class variable, rather than an
instance variable. The multiple use
of a reserved word can be confusing
particularly to those learning the
language.
did you understand? :(
I want to write some text about initializaton too, which i can later link to.
First the list of possibilities.
Namespace Static
Class Static
Local Static
Namespace Static
There are two initialization methods. static (intended to happen at compile time) and dynamic (intended to happen at runtime) initialization.
Static Initialization happens before any dynamic initialization, disregarding of translation unit relations.
Dynamic Initiaization is ordered in a translation unit, while there is no particular order in static initialization. Objects of namespace scope of the same translation unit are dynamically initialized in the order in which their definition appears.
POD type objects that are initialized with constant expressions are statically initialized. Their value can be relied on by any object's dynamic initialization, disregarding of translation unit relations.
If the initialization throws an exception, std::terminate is called.
Examples:
The following program prints A(1) A(2)
struct A {
A(int n) { std::printf(" A(%d) ", n); }
};
A a(1);
A b(2);
And the following, based on the same class, prints A(2) A(1)
extern A a;
A b(2);
A a(1);
Let's pretend there is a translation unit where msg is defined as the following
char const *msg = "abc";
Then the following prints abc. Note that p receives dynamic initialization. But because the static initialization (char const* is a POD type, and "abc" is an address constant expression) of msg happens before that, this is fine, and msg is guaranteed to be correctly initialized.
extern const char *msg;
struct P { P() { std::printf("%s", msg); } };
P p;
Dynamic initialization of an object is not required to happen before main at all costs. The initialization must happen before the first use of an object or function of its translation unit, though. This is important for dynamic loadable libraries.
Class Static
Behave like namespace statics.
There is a bug-report on whether the compiler is allowed to initialize class statics on the first use of a function or object of its translation unit too (after main). The wording in the Standard currently only allows this for namespace scope objects - but it seems it intends to allow this for class scope objects too. Read Objects of Namespace Scope.
For class statics that are member of templates the rule is that they are only initialized if they are ever used. Not using them will not yield to an initialization. Note that in any case, initialization will happen like explained above. Initialization will not be delayed because it's a member of a template.
Local Static
For local statics, special rules happen.
POD type objects initialized with constant expression are initialized before their block in which they are defined is entered.
Other local static objects are initialized at the first time control passes through their definition. Initialization is not considered to be complete when an exception is thrown. The initialization will be tried again the next time.
Example: The following program prints 0 1:
struct C {
C(int n) {
if(n == 0)
throw n;
this->n = n;
}
int n;
};
int f(int n) {
static C c(n);
return c.n;
}
int main() {
try {
f(0);
} catch(int n) {
std::cout << n << " ";
}
f(1); // initializes successfully
std::cout << f(2);
}
In all the above cases, in certain limited cases, for some objects that are not required to be initialized statically, the compiler can statically initialize it, instead of dynamically initializing it. This is a tricky issue, see this answer for a more detailed example.
Also note that the order of destruction is the exact order of the completion of construction of the objects. This is a common and happens in all sort of situations in C++, including in destructing temporaries.
Exactly like regular static (global) variables.
It gets constructed at the same time the global variables get constructed and destructed along with the globals as well.
Simply speaking:
A static member variable is constructed when the global variables are constructed. The construction order of global variables is not defined, but it happens before the main-function is entered.
Destruction happens when global variables are destroyed.
Global variables are destroyed in the reversed order they were constructed; after exiting the main-function.
Regards,
Ovanes
P.S.: I suggest to take a look at C++-Standard, which explains (defines) how and when global or static member variables are constructed or destructed.
P.P.S.: Your code only declares a static member variable, but does not initialize it. To initialize it you must write in one of the compilation units:
std::vector Test::staticVector;
or
std::vector Test::staticVector=std::vector(/* ctor params here */);
Some specific VC++ information in case that's what you're using:
Static class variables construction occurs at same time as other static/global variables.
In windows, the CRT startup function is responsible for this construction.
This is the actual entry point of most programs you compile (it is the function which calls your Main/Winmain function).
In addition, it is responsible for initializing the entire C runtime support (for example you need it to use malloc).
The order of construction is undefined, however when using the microsoft VC compiler the order of construction for basic types will be OK, for example it is legal and safe to write
statics.h:
... MyClass declaration ...
static const int a;
static int b;
static int ar[];
}
statics.cpp:
const int MyClass::a = 2;
int MyClass::b = a+3;
int MyClass::ar[a] = {1,2}