C++中全局变量的初始化全过程

 更新时间:2024年08月21日 10:13:13   作者:vcpro126  
这篇文章主要介绍了C++全局变量的初始化全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

C++全局变量的初始化过程

全局变量在main()前完成初始化(调用构造函数)

在调用构造函数前,全局变量已分配空间,内存全0

多个全局变量的初始化,按照代码编译的顺序

注意:全局变量被访问前可能它还没有调用构造函数初始化。

如果一个项目中,多个dll都用到一个全局变量在共同的lib中,则每个dll都有一个独立的全局变量(地址不同),每个全局变量会初始化。

如下代码,A里面访问了全局变量g_b, 改变全局变量g_a,g_b的顺序 会导致g_b.b的输出结果不同。

如按照A g_a; B g_b 的顺序定义,编译器会先调用A()时,这时g_b还没有调用B(), g_b.b=0,然后赋值 g_b.b=101;

然后调用B(),g_b.b的值被改成1.

#include <istream>
using namespace std;

class B {
 public:
  int b = 1;
};

extern B g_b;
class A {
 public:
  int a = 0;
  A() {
    g_b.b = 101;
  }
};

#if 0
B g_b;
A g_a;
#else
A g_a;
B g_b;
#endif
int main() {
  printf("g_b=%d\n", g_b); //AB: g_b=1; BA: g_b=101
  return 0;
}

C++全局变量初始化的顺序

虽然一直强调不要用全局变量。但是对于特殊的应用场合,还是有全局变量的使用(如某些多进程、多线程的共享资源),我们希望在首次运行(载入)时,系统能够帮助我们进行一些必要的初始化。

If a program starts a thread (30.3), the subsequent initialization of a variable is unsequenced with respect to the initialization of a variable defined in a different translation unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization of a variable defined in a different translation unit. If a program starts a thread, the subsequent unordered initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise, the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic initialization

对不同的源文件中的全局变量,标准C++对全局变量初始化的顺序并没有要求。对于同一个原文件中,全局变量按照定义先后顺序初始化。

对于堆类型的全局变量的创建和构造,可能在一个构造函数中调用另一个未构造的全局变量,通常会检查另一个指针是否有效,并在无效时构造那个对象。这就出现一个问题:

一个指针在构造之前,被初始化。c/c++运行时,仍然会再次构造那个指针(全局变量为空指针)。

这会引发资源泄露,甚至运行时错误。

解决措施。虽然按需创建是一种很好的思路。但是必须符合c/c++运行机制。解决方式就是不使用堆创建对象,而是使用栈内存对象(c++内部大使用致双检查锁的方式保证构造一次)。我们也可以自己实现双检查锁的思路,但是我们使用的锁的创建过程,本身就是需要双检查锁定的,这是自相矛盾的。

参考C++标准具体介绍:(原文:Storage class specifiers - cppreference.com

Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

If the initialization throws an exception, the variable is not considered to be initialized, and initialization will be attempted again the next time control passes through the declaration.

If the initialization recursively enters the block in which the variable is being initialized, the behavior is undefined.

If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once (similar behavior can be obtained for arbitrary functions with std::call_once).

Note: usual implementations of this feature use variants of the double-checked locking pattern, which reduces runtime overhead for already-initialized local statics to a single non-atomic boolean comparison.

(since C++11)

The destructor for a block-scope static variable is called at program exit, but only if the initialization took place successfully.

Function-local static objects in all definitions of the same inline function (which may be impli

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • C++实现大整数乘法(字符串乘法)

    C++实现大整数乘法(字符串乘法)

    这篇文章主要为大家详细介绍了C++实现大整数乘法、字符串乘法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • C语言中getch()函数详解及简单实例

    C语言中getch()函数详解及简单实例

    这篇文章主要介绍了C语言中getch()函数详解及简单实例的相关资料,需要的朋友可以参考下
    2017-03-03
  • C语言进阶可变参数列表

    C语言进阶可变参数列表

    这篇文章主要为大家介绍了C语言进阶可变参数列表的示例详解有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • C/C++整数乘积的溢出问题的解决

    C/C++整数乘积的溢出问题的解决

    整数乘积的溢出问题是指两个整数相乘得到的结果超过了所能表示的数据类型的范围,本文给大家介绍了C/C++整数乘积的溢出问题的解决,需要的朋友可以参考下
    2024-02-02
  • C++临时性对象的生命周期详细解析

    C++临时性对象的生命周期详细解析

    临时性对象的被摧毁,应该是对完整表达式(full-expression)求值过程中的最后一个步骤。该完整表达式造成临时对象的产生
    2013-09-09
  • C++11原子操作详解

    C++11原子操作详解

    这篇文章主要为大家介绍了C++的原子操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-11-11
  • MATLAB中count函数用法示例代码

    MATLAB中count函数用法示例代码

    这篇文章主要介绍了MATLAB中的count函数,用于计算字符串中模式的出现次数,它可以处理单个模式和模式数组,并且可以忽略大小写,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-02-02
  • QT打包发布全流程(图文教程)

    QT打包发布全流程(图文教程)

    本文主要介绍了QT打包发布全流程,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C语言实现六边形扫雷游戏的示例代码

    C语言实现六边形扫雷游戏的示例代码

    所谓六边形扫雷,就是没有扫雷模式的消零算法,每一个安全的点都需要单独挖出来,一次显示一个格子,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • C++标准模板库STL深入讲解

    C++标准模板库STL深入讲解

    STL提供了一组表示容器、迭代器、函数对象和算法的模板。容器是一个与数组类似的单元,可以存储若干个值。STL容器是同质的,即存储的值的类型相同:算法是完成特定任务(如对数组进行排序或在链表中查找特定值)的处方
    2022-12-12

最新评论