简体   繁体   English

const变量何时初始化

[英]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: 但存在潜在的死亡陷阱:

  • It might not happen before other static variables are initialised; 在初始化其他静态变量之前可能不会发生。 you'll get evil bugs if their constructors access it. 如果他们的构造函数访问它,您将得到邪恶的错误。
  • It might not happen before main begins, if it's defined in a different unit. 如果它是在另一个单元中定义的,则可能在main开始之前不会发生。
  • It might not happen at all, if you never call any function in the same unit. 如果您从未在同一单元中调用任何函数,则可能根本不会发生。

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.

相关问题 何时初始化全局静态const变量? - When are global static const variables being initialized? 可以在方法而不是构造函数中初始化类的const成员变量吗? - Can const member variable of class be initialized in a method instead of constructor? 是静态常量字符串成员变量总是在使用前初始化? - is static const string member variable always initialized before used? 应该在c ++头文件中初始化const静态变量吗? - Should a const static variable be initialized in a c++ header file? 当值是非常量但使用常量表达式初始化时使用constexpr吗? - Using constexpr when a value is non-const but initialized with a constant expression? 当我将const int声明/初始化为5时,它被初始化为一个大数字 - When I declare/initialize a const int as 5 it is initialized as a large number 在类中初始化const - Initialized const in a class `thread_local` 全局变量何时初始化? - When is a `thread_local` global variable initialized? 在switch语句中初始化时未定义的变量? - Undefined variable when initialized in switch statement? 在C ++标准中它表示必须初始化const内置类型变量的定义? - Where in the C++ Standard does it say that the definition of a const built-in type variable must be initialized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM