[英]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.
So it makes sense to me why b
is dynamically initialized and a
is statically initialized. 所以,对我来说很有意义,为什么
b
是动态初始化, a
是静态初始化。
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? 但是,如果
a
和b
具有自动存储持续时间(也许它们已在main()
初始化),那么你是否仍可以将其初始化称为静态或动态? 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. cppreference声明初始化器可以调用(一些初始化,比如值初始化等),但是在本文后面,他们提到了静态和动态初始化,就好像这两个是一些初始化的更通用的名称。 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. 静态和动态初始化描述了加载二进制文件并进入main准备运行时的过程。
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
但是如果a和b有自动存储持续时间怎么办呢
The simple case when a is an automatic variable of limited scope. 当a是有限范围的自动变量时的简单情况。
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. 这不能被静态初始化,因为编译器不会知道在何处初始化a,因为每次都会有所不同,并且在每个调用它的线程上都是如此。
The compiler will be able to initialize a with something like. 编译器将能够用类似的东西初始化a。
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. 由于_addr_of_a在运行时是未知的,并且值12嵌入在代码中,因此不会进行静态初始化的情况。
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. 动态行为更成问题 - 它假定一个通常不暴露其实现的函数具有较慢的执行时间,并且是一个
constexpr
用于在结果开始时为缓存赋值。 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. 局部变量可能存在类似的模式,但它们不属于
static
和dynamic
初始化的技术定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.