什么时候静态和全局变量初始化? [英] When are static and global variables initialized?

查看:312
本文介绍了什么时候静态和全局变量初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 我知道 static 全局是在 main 函数之前构建的。但是你知道,在 C 之前,在之前没有这样的初始化过程 / code>。

In C++ I know static and global objects are constructed before the main function. But as you know, in C, there is no such kind initialization procedure before main.

例如,在我的代码中:

int global_int1 = 5;
int global_int2;
static int static_int1 = 4;
static int static_int2;




  • 初始化这四个变量何时生效?

  • 在编译期间存储如 5 4 的初始化值?如何在初始化时管理它们?

    • When are these four variables initialized?
    • Where values for initialization like 5 and 4 are stored during compilation? How to manage them when initialization?


    • 澄清第二个问题。


      Clarification of 2nd question.


      • 在我的代码中,我使用 5 global_int1 ,因此编译器如何分配 5 global_int ?例如,也许编译器首先将 5 值存储在某处(即表),并在初始化开始时获取此值。

      • 关于如何在初始化时管理它们?,它是真实的模糊,我自己没有如何解释。有时,解释一个问题不容易。忽略它,因为我还没有充分掌握这个问题。

      • In my code I use 5 to initialize global_int1, so how can the compiler assign 5 to global_int? For example, maybe the compiler first store the 5 value at somewhere (i.e. a table), and get this value when initialization begins.
      • As to "How to manage them when initialization?", it is realy vague and I myself does not how to interpret yet. Sometimes, it is not easy to explain a question. Overlook it since I have not mastered the question fully yet.

      推荐答案

      ,我假设你的意思是
      静态生命周期定义在命名空间范围的对象。当这样的对象
      用本地作用域定义时,规则略有不同。

      By static and global objects, I presume you mean objects with static lifetime defined at namespace scope. When such objects are defined with local scope, the rules are slightly different.

      正式来说,C ++在三个阶段初始化这些变量:
      1.零初始化
      2.静态初始化
      3.动态初始化
      该语言还区分需要
      动态初始化的变量和需要static
      初始化的变量:所有静态对象(具有静态
      生命周期的对象)首先被初始化,然后对象static
      初始化,然后动态初始化

      Formally, C++ initializes such variables in three phases: 1. Zero initialization 2. Static initialization 3. Dynamic initialization The language also distinguishes between variables which require dynamic initialization, and those which require static initialization: all static objects (objects with static lifetime) are first zero initialized, then objects with static initialization are initialized, and then dynamic initialization occurs.

      作为一个简单的初步近似,动态初始化意味着
      ,必须执行;通常,static
      初始化不会。因此:

      As a simple first approximation, dynamic initialization means that some code must be executed; typically, static initialization doesn't. Thus:

      extern int f();
      
      int g1 = 42;    //  static initialization
      int g2 = f();   //  dynamic initialization
      

      另一个近似值是静态初始化是
      C支持具有静态生存期的变量),动态
      一切。

      Another approximization would be that static initialization is what C supports (for variables with static lifetime), dynamic everything else.

      编译器这样做取决于
      初始化,其中可执行文件
      从磁盘加载到内存中,静态
      初始化的值是磁盘映像的一部分,系统从磁盘直接加载
      。在经典的Unix
      系统上,全局变量将被分为三个段:

      How the compiler does this depends, of course, on the initialization, but on disk based systems, where the executable is loaded into memory from disk, the values for static initialization are part of the image on disk, and loaded directly by the system from the disk. On a classical Unix system, global variables would be divided into three "segments":

      我怀疑很多现代系统仍然使用
      类似的东西。

      I suspect that a lot of modern systems still use something similar.

      编辑:

      另外一句话:上面提到的是C ++ 03。对于现有的
      程序,C ++ 11可能不会改变任何东西,但它会
      add constexpr (这意味着一些用户定义的函数
      仍然可以是静态初始化)和线程局部变量
      ,它打开了一个全新的蠕虫病毒。

      One additional remark: the above refers to C++03. For existing programs, C++11 probably doesn't change anything, but it does add constexpr (which means that some user defined functions can still be static initialization) and thread local variables, which opens up a whole new can of worms.

      这篇关于什么时候静态和全局变量初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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