简体   繁体   English

静态和动态初始化仅适用于非局部变量吗?

[英]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. 所以,对我来说很有意义,为什么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? 但是,如果ab具有自动存储持续时间(也许它们已在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. 局部变量可能存在类似的模式,但它们不属于staticdynamic初始化的技术定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何理解允许实现在某些情况下将非局部变量的动态初始化视为静态初始化? - How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases? 在不同的翻译单元中对带有静态存储持续时间的相关非局部常量浮点变量进行常量初始化 - Constant initialization of dependent non-local constant float variables w/ static storage duration in different translation units 非局部变量的动态初始化是线程安全的吗 - Is the dynamic initialization of the non-local variable thread-safe 非局部非内联变量的初始化:它是否严格在调用main()函数之前进行? - The initialization of non-local non-inline variables: does it take place strictly before the `main()` function call? 静态初始化局部变量 - Static initialization of local variables 关于非本地静态变量初始化的规则是什么? - What are the rules regarding initialization of non-local statics? 在本地定义的结构中模拟非本地(或自由)变量 - Simulating non-local (or free) variables in a locally defined struct 如果全局 function 使用非局部变量是否正确,那么它是一个闭包? - Is it correct that if a global function uses non-local variables, then it's a closure? 在 C++ 中,哪些链接被授予非局部成员变量? - Which linkages are granted to non-local member variables in c++? 静态变量的动态初始化阶段 - Dynamic initialization phase of static variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM