C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++全局变量的初始化

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

作者: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

总结

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

您可能感兴趣的文章:
阅读全文