静态变量和全局变量什么时候初始化?

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

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

问题描述

C++ 我知道 staticglobal 对象是在 main 函数之前构造的.但是如你所知,在C中,在main之前并没有这样的initialization procedure.

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;

  • 这四个变量何时初始化?
  • 54 等初始化值在编译期间存储在哪里?初始化时如何管理?
    • 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,那么编译器如何assign 5global_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.动态初始化该语言还区分需要的变量动态初始化,以及那些需要静态的初始化:所有静态对象(静态对象生命周期)首先零初始化,然后是静态对象初始化被初始化,然后动态初始化发生.

      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.

      作为一个简单的第一近似,动态初始化意味着必须执行一些代码;通常,静态初始化没有.因此:

      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 可能不会改变任何东西,但它确实添加 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.

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

本文标题为:静态变量和全局变量什么时候初始化?

基础教程推荐