Static and dynamic initialization apply only to non-local variables?

Here is the code:

int factorial(int n) { if ( n < 0 ) return -1; //indicates input error else if ( n == 0 ) return 1; else return n * factorial(n-1); } int const a = 10 ; //static initialization //10 is known at compile time. Its 10! int const b = factorial(8); //dynamic initialization //factorial(8) isn't known at compile time, //rather it computed at runtime. 

(stolen from here )

So, for me it makes sense why b dynamically initialized and a statically initialized.

But what if a and b had an automatic storage duration (perhaps they were initialized in main() ), could you still call their initialization static or dynamic? Because for me they sound like a more general name for initialization than, for example, for copy initialization.

Also, I read this and can anyone tell me why they did not explain directly what static and dynamic initialization is? I mean, it looks like they only explained in what situations they happen, but maybe there is a reason why?

cppreference indicates that the initializer can call (some initializations, such as initializing values, etc.), but later in the article static and dynamic initializations are mentioned, as if these two were more common names for some initializations. This may seem confusing, but here I have illustrated what I understand:

enter image description here

(not the most beautiful thing)

+5
source share
1 answer

Static and dynamic initialization describes the process of loading a binary file and moving to the point when main is ready to go.

static initialization describes the information that the compiler can execute at compile time, and allows you to fix the values ​​in binary format, so when the binary is loaded by the operating system, it has the correct value.

dynamic initialization describes code that is inserted by the compiler before the main starts, which initializes information that the compiler could not calculate. This may be due to the fact that it is directly related to the code or that it refers to information that was not visible to the compiler at compile time.

But what if a and b had an automatic storage duration

The simple case when a is an automatic variable of a bounded domain.

 int a = 12; 

This cannot be statically initialized because the compiler will not know where to initialize a, since it will be different every time, and from every thread that called it.

The compiler will be able to initialize with something like.

  mov (_addr_of_a), 12 

Since _addr_of_a is unknown before execution, and the value 12 is embedded in the code, the case of static initialization will not be executed.

More complicated cases ...

 int a[] = { /* some integer values */ }; 

Perhaps this will be implemented by the compiler as a mixture of static and dynamic code, as shown below.

 static int a_init = { /* some integer values */ }; memcpy( a, a_init, length_in_bytes_of_a ); 

So in some cases there will be a "leak" from static initialization at runtime.

Dynamic behavior is more problematic - it assumes that a function that usually does not expose its implementation has both slow execution time and constexpr in order to give importance to caching at the beginning of the result. I have not seen this optimization happen.

Static and dynamic initialization are technical terms that describe the process of creating a running program. Similar patterns may exist for local variables, but they will not fall into the technical definition of static and dynamic initialization.

+2
source

Source: https://habr.com/ru/post/1270728/


All Articles