C ++全局变量初始化顺序 [英] C++ global variable initialization order

查看:163
本文介绍了C ++全局变量初始化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下代码示例的作用及其效果:

I don't understand what the following code example does and how it does it:

#include <stdio.h>

int f();

int a = f(); // a exists just to call f

int x = 22;

int f() {
    ++x;
    return 123; // unimportant arbitrary number
}

int main() {
    printf("%d\n", x);
}

当它运行时打印 23

When this is ran it prints 23, which is the intuitive answer.

但是在C ++中,全局变量是应按照定义的顺序初始化。这意味着 a 应该在 x 之前初始化,因为它在 x 。如果是这种情况,则在初始化 x 之前必须调用函数 f ,因为调用 f a 的一部分。

However in C++, global variables are supposed to be initialized in order of definition. That would mean that a should be initialized before x, because it is defined before x. If that was the case, then the function f would have to be called before x was initialized, because the call to f is a part of a's definition.

如果 f 在初始化 x 之前确实被调用,那么意味着 f 会尝试增加 x - 其结果我不确定(很可能是UB,或一些乱码值)。然后, a 初始化后, x 将初始化为 22 并且程序将打印出 22

If f is indeed called before x is initialized, that would mean that f would try to increment x -- the result of which I'm not really certain of (most likely UB, or some gibberish value). Then, after a is initialized, x would be initialized to 22 and the program would print out 22.

显然这不是会发生什么。但是什么?这个代码实际上是做什么的?

看起来像 x 设置为 22 之前 a = f()被评估,但这将意味着初始化的顺序颠倒关于什么是初始化,或什么时候发生)。

It definitely seems like x is set to 22 before a = f() is evaluated, but that would mean that the order of initialization is reversed (I could also be wrong about what initialization is, or when it happens).

推荐答案

问题有点微妙;请参考C ++ 11 3.6.2了解详细信息。

The issue is a little bit subtle; please refer to C++11 3.6.2 for details.

对我们来说重要的是,有两个阶段的初始化非本地变量静态存储持续时间(或者口语化中的全局变量):静态初始化阶段动态初始化阶段。静态阶段首先。它看起来像这样:

What matters for us is that there are two phases of initialization of "non-local variables with static storage duration" (or "global variables" in colloquial parlance): the static initialization phase and the dynamic initialization phase. The static phase comes first. It looks like this:

int a = 0;
int x = 22;

动态初始化随后运行:

a = f();

关键是静态初始化不是运行 - 它只包括设置值它们在编译时是已知的,因此在任何执行发生之前已经设置了这些值。什么使初始化 int x = 22; static是初始化器是一个常量表达式。

The point is that static initialization doesn't "run" at all - it only consists of setting values that are known at compile time, so those values are already set before any execution happens. What makes the initialization int x = 22; static is that the initializer is a constant expression.

有些情况下,动态初始化可能会被提升到静态阶段(但不是必须),但这不是这些情况之一,因为它不满足要求初始化的动态版本不会改变命名空间作用域的任何其他对象的值,因为

There are cases where dynamic initialization may be hoisted to the static phase (but does not have to), but this is not one of those cases, because it does not meet the requirement that


>

the dynamic version of the initialization does not change the value of any other object of namespace scope prior to its initialization

当这个提升发生时,允许生成的初始值可能与不发生时不同。在一个这样的不确定初始化的标准中有一个例子。

When this hoisting happens, it is permissible that the resulting initial values can be different from if it didn't happen. There's an example in the standard for one such "indeterminate" initialization.

这篇关于C ++全局变量初始化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆