简体   繁体   中英

Do static and dynamic initialization only apply 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's computed at runtime.

(stolen from here )

So it makes sense to me why b is dynamically initialized and a is statically initialized.

But what if a and b had automatic storage duration(maybe they had been initialized in main() ), could you then still call their initialization either static or dynamic? Because, to me, they sound like a more general name for initialization than for example Copy initialization.

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

cppreference states the initializer may invoke (some intializations, like value initialization etc.), but later in the article, they mention static and dynamic initialization as if those two were more general names for some initializations. This could sound confusing, but here I have illustrated what I understand:

在此输入图像描述

(not the most beautiful thing)

Static and dynamic initialization describe the process of loading a binary and getting to the point when main is ready to run.

static initialization describes the information the compiler can work out at compile time, and allows for fixed values to be stored in the binary so at the point when the binary is loaded by the operating system, it has the correct value.

dynamic initialization describes the code which is inserted by the compiler before main runs, which initializes the information which the compiler was unable to calculate. That may be because it involves code directly, or that it refers to information which was not visible to the compiler at compile time.

But what if a and b had automatic storage duration

The simple case when a is an automatic variable of limited scope.

int a = 12;

This could not be statically initialized, because the compiler will not know where to initialize a, as it would be different each time, and on each thread which called it.

The compiler will be able to initialize a with something like.

  mov (_addr_of_a), 12

As _addr_of_a is unknown until runtime, and the value 12 is embedded in the code, a case for statically initializing it would not be done.

More complex cases ...

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

This is possibly going to be implemented by the compiler as a mixture of static and dynamic code as below.

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

So some cases there will be "leakage" from static initialization into runtime behaviour.

Dynamic behavior is more problematic - it assumes that a function which does not normally expose its implementation, has both a slow execution time, and is a constexpr to give value to the caching at start of the result. I have not seen this optimization occur.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM