[英]When is const variable initialized
When and how are const variables initialized in C/C++? 何时以及如何在C / C ++中初始化const变量? I am curious about particular types:
我对特定类型感到好奇:
1) const static member of a class
2) function const local variable
3) const global variable
I mean of course application run time not source code way of initializing them. 我的意思是,当然,应用程序运行时不是初始化它们的源代码方式。
1) const static member of a class
1)const静态类的成员
If it's a simple type initialised with a constant expression, then it's initialised during the static initialisation phase, before any user code is run. 如果它是用常量表达式初始化的简单类型,则在运行任何用户代码之前,在静态初始化阶段对其进行初始化。
Otherwise, if it's a class type with a non-trivial constructor, or has a non-constant initialiser, then it's initialised during the dynamic initialisation phase. 否则,如果它是具有非平凡构造函数的类类型,或者具有非常量初始化程序,则将在动态初始化阶段对其进行初始化 。 This will happen before any function that's defined in the same translation unit is called;
这将在调用同一翻译单元中定义的任何函数之前发生; but there are potential deathtraps:
但存在潜在的死亡陷阱:
main
begins, if it's defined in a different unit. main
开始之前不会发生。 2) function const local variable
2)函数const局部变量
If it's static, then it's initialised the first time the program reaches its definition. 如果它是静态的,则在程序第一次达到其定义时进行初始化。 If it's automatic, it's initialised every time the program reaches it, and destroyed when it goes out of scope.
如果是自动的,则每次程序到达时都会对其进行初始化,并在超出范围时将其销毁。
3) const global variable
3)const全局变量
Same as the first. 与第一个相同。 Both have static storage duration, and so are initialised and destroyed according to the same rules.
两者均具有静态存储期限,因此将根据相同规则进行初始化和销毁。
Notes: 笔记:
Since you're asking about two different languages: in C, there's no such thing as "dynamic initialisation", and all non-local static variables are initialised before any user code runs. 因为您要问两种不同的语言:在C语言中,没有“动态初始化”之类的东西,并且所有非局部静态变量都在运行任何用户代码之前进行了初始化。
Being const
has no effect on when or how a variable is initialised. 设为
const
不会影响何时或如何初始化变量。
Storage duration tells you what rules apply for when the variable in a program is allocated and deallocated. 存储持续时间告诉您在分配和释放程序中的变量时适用的规则。 To answer your question is to specify storage duration for each case that you mentioned.
回答您的问题是为您提到的每种情况指定存储期限。
1) const static member of a class : static storage duration,
simple type - static initialisation,
class with non-trivial constructor - dynamic
initialisation
2) function const local variable : automatic storage duration ( but static
duration if it is static const),
automatic variable is initialized each time
the code is run,
static variable is initialized the first
time code is run,
3) const global variable : static storage duration
simple type - static initialisation,
class with non-trivial constructor - dynamic
initialisation
Explanation of storage durations: 储存期限的说明 :
automatic storage duration
自动储存期限
The variable is allocated at the beginning of the enclosing code block and deallocated on end.
该变量在封闭代码块的开头分配,并在结尾释放。 All non-global variables have this storage duration, except those declared static, extern or thread_local.
除声明为static,extern或thread_local的变量外,所有非全局变量均具有此存储期限。
static storage duration
静态储存期
The variable is allocated when the program begins and deallocated when the program ends.
在程序开始时分配变量,在程序结束时释放变量。 Only one instance of the variable exists.
变量只有一个实例存在。 All global variables have this storage duration, plus those declared with static or extern.
所有全局变量都具有此存储期限,加上使用静态或外部声明的变量。
thread storage duration
线程存储时间
The variable is allocated when the thread begins and deallocated when the thread ends.
该变量在线程开始时分配,并在线程结束时释放。 Each thread has its own instance of the variable.
每个线程都有其自己的变量实例。 Only variables declared thread_local have this storage duration.
只有声明为thread_local的变量才具有此存储时间。 thread_local can appear together with static or extern to adjust linkage.
thread_local可以与static或extern一起出现以调整链接。 (since C++11)
(自C ++ 11起)
dynamic storage duration
动态存储期限
The variable is allocated and deallocated per request by using dynamic memory allocation functions.
通过使用动态内存分配功能,可以按请求分配和释放该变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.