Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guaranteeing static (constant) initialization of static objects

Tags:

Overarching question is: how can a programmer make sure that his non-local static variables are initialized via static initialization and not via dynamic initialization?

As zero-initialization is done always then one should look at the constant initialization.

3.6.2.2 A constant initializer for an object o is an expression that is a constant expression, except that it may also invoke constexpr constructors for o and its subobjects even if those objects are of non-literal class types [ Note: such a class may have a non-trivial destructor —end note ]. Constant initialization is performed:

— if each full-expression (including implicit conversions) that appears in the initializer of a reference with static or thread storage duration is a constant expression (5.19) and the reference is bound to an lvalue designating an object with static storage duration or to a temporary (see 12.2);

— if an object with static or thread storage duration is initialized by a constructor call, and if the initialization full-expression is a constant initializer for the object;

— if an object with static or thread storage duration is not initialized by a constructor call and if either the object is value-initialized or every full-expression that appears in its initializer is a constant expression.

I omitted the reference as it is not important in my case. How I understand the standard is that there are 3 cases:

  1. ctor
  2. no-ctor and value initialization
  3. no-ctor and constant expression

Let's say I have a following class:

struct X {   bool flag = false;   // = {} will break VS2013 CTP so in that case use the   // regular ctor, which sadly still can't be declared constexpr   std::aligned_storage<sizeof(int), alignof(int)>::type storage = {}; }; 

As far as I can say this class is perfectly valid for constant initialization (each element can be constantly initialized). Is this true?

Does this class require a constexpr constructor?

Is constant initialization guaranteed for C++11 as well as C++98?

Side question: When will the static initialization done in case of so/dll? During the load time, or it might be delayed even further?

like image 296
Red XIII Avatar asked Oct 03 '14 08:10

Red XIII


People also ask

What is meant by static initialization of objects?

You initialize a static object with a constant expression, or an expression that reduces to the address of a previously declared extern or static object, possibly modified by a constant expression.

How do you initialize a static object in C++?

We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

How are static variables initialized?

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.

Where should static variables be initialized?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).


1 Answers

It would be good to know the purpose behind this question. And also whether your concern is allocation or specifically initialization.

However, the type of initialization shouldn't matter because the required space is allocated at compile time. Depending on how you define the variable, it will end up either in .bss or .data section.

Initialization, as you know, is only to ensure a specific content in the memory before it is first used. If you do not define a constructor that allocates dynamic memory then there won't be any dynamic allocation (if that is your concern).

For simple constructors, I believe the compiler will generate inline code and use the same to initialize the object at compile time (I am not sure what the standard talks about the same but it is possible that it is tool chain dependent.) With complex constructors, non-local static objects will be initialized when the image is loaded in memory and local static objects will be initialized when the stack frame is loaded. In any cases you should find the object in a known state before first use.

like image 123
RcnRcf Avatar answered Oct 31 '22 15:10

RcnRcf