• <small id='7QyxG'></small><noframes id='7QyxG'>

    1. <legend id='7QyxG'><style id='7QyxG'><dir id='7QyxG'><q id='7QyxG'></q></dir></style></legend>
      <i id='7QyxG'><tr id='7QyxG'><dt id='7QyxG'><q id='7QyxG'><span id='7QyxG'><b id='7QyxG'><form id='7QyxG'><ins id='7QyxG'></ins><ul id='7QyxG'></ul><sub id='7QyxG'></sub></form><legend id='7QyxG'></legend><bdo id='7QyxG'><pre id='7QyxG'><center id='7QyxG'></center></pre></bdo></b><th id='7QyxG'></th></span></q></dt></tr></i><div id='7QyxG'><tfoot id='7QyxG'></tfoot><dl id='7QyxG'><fieldset id='7QyxG'></fieldset></dl></div>
      <tfoot id='7QyxG'></tfoot>

          <bdo id='7QyxG'></bdo><ul id='7QyxG'></ul>

        编译器是如何实现静态变量初始化的?

        How is static variable initialization implemented by the compiler?(编译器是如何实现静态变量初始化的?)

              <legend id='V5YV8'><style id='V5YV8'><dir id='V5YV8'><q id='V5YV8'></q></dir></style></legend>
              • <bdo id='V5YV8'></bdo><ul id='V5YV8'></ul>
                <i id='V5YV8'><tr id='V5YV8'><dt id='V5YV8'><q id='V5YV8'><span id='V5YV8'><b id='V5YV8'><form id='V5YV8'><ins id='V5YV8'></ins><ul id='V5YV8'></ul><sub id='V5YV8'></sub></form><legend id='V5YV8'></legend><bdo id='V5YV8'><pre id='V5YV8'><center id='V5YV8'></center></pre></bdo></b><th id='V5YV8'></th></span></q></dt></tr></i><div id='V5YV8'><tfoot id='V5YV8'></tfoot><dl id='V5YV8'><fieldset id='V5YV8'></fieldset></dl></div>
                <tfoot id='V5YV8'></tfoot>
              • <small id='V5YV8'></small><noframes id='V5YV8'>

                    <tbody id='V5YV8'></tbody>
                  本文介绍了编译器是如何实现静态变量初始化的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对函数内静态变量的底层实现很好奇.

                  I'm curious about the underlying implementation of static variables within a function.

                  如果我声明一个基本类型(char、int、double 等)的静态变量,并给它一个初始值,我想编译器会在程序的最开始简单地设置该变量的值在 main() 被调用之前:

                  If I declare a static variable of a fundamental type (char, int, double, etc.), and give it an initial value, I imagine that the compiler simply sets the value of that variable at the very beginning of the program before main() is called:

                  void SomeFunction();
                  
                  int main(int argCount, char ** argList)
                  {
                      // at this point, the memory reserved for 'answer'
                      // already contains the value of 42
                      SomeFunction();
                  }
                  
                  void SomeFunction()
                  {
                      static int answer = 42;
                  }
                  

                  但是,如果静态变量是类的实例:

                  However, if the static variable is an instance of a class:

                  class MyClass
                  {
                      //...
                  };
                  
                  void SomeFunction();
                  
                  int main(int argCount, char ** argList)
                  {
                      SomeFunction();
                  }
                  
                  void SomeFunction()
                  {
                      static MyClass myVar;
                  }
                  

                  我知道在第一次调用该函数之前它不会被初始化.由于编译器无法知道该函数何时会被第一次调用,它是如何产生这种行为的呢?它本质上是否在函数体中引入了一个 if 块?

                  I know that it will not be initialized until the first time that the function is called. Since the compiler has no way of knowing when the function will be called for the first time, how does it produce this behavior? Does it essentially introduce an if-block into the function body?

                  static bool initialized = 0;
                  if (!initialized)
                  {
                      // construct myVar
                      initialized = 1;
                  }
                  

                  推荐答案

                  在我看到的编译器输出中,函数局部静态变量完全按照你的想象进行初始化.

                  In the compiler output I have seen, function local static variables are initialized exactly as you imagine.

                  请注意,通常这不是以线程安全的方式完成.因此,如果您的函数具有可能从多个线程调用的静态局部变量,则应考虑到这一点.在调用任何其他函数之前在主线程中调用一次该函数通常会奏效.

                  Note that in general this is not done in a thread-safe manner. So if you have functions with static locals like that that might be called from multiple threads, you should take this into account. Calling the function once in the main thread before any others are called will usually do the trick.

                  我应该补充一点,如果本地静态的初始化是通过一个简单的常量,比如你的例子,编译器不需要经历这些旋转——它可以只在图像中或在 之前初始化变量main() 就像一个常规的静态初始化(因为你的程序无法区分).但是如果你用一个函数的返回值来初始化它,那么编译器几乎必须测试一个标志,表明初始化是否已经完成或等价的东西.

                  I should add that if the initialization of the local static is by a simple constant like in your example, the compiler doesn't need to go through these gyrations - it can just initialize the variable in the image or before main() like a regular static initialization (because your program wouldn't be able to tell the difference). But if you initialize it with a function's return value, then the compiler pretty much has to test a flag indicating if the initialization has been done or something equivalent.

                  这篇关于编译器是如何实现静态变量初始化的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                  1. <legend id='09X4n'><style id='09X4n'><dir id='09X4n'><q id='09X4n'></q></dir></style></legend>

                      <tbody id='09X4n'></tbody>

                      <small id='09X4n'></small><noframes id='09X4n'>

                      • <bdo id='09X4n'></bdo><ul id='09X4n'></ul>
                          <tfoot id='09X4n'></tfoot>
                        • <i id='09X4n'><tr id='09X4n'><dt id='09X4n'><q id='09X4n'><span id='09X4n'><b id='09X4n'><form id='09X4n'><ins id='09X4n'></ins><ul id='09X4n'></ul><sub id='09X4n'></sub></form><legend id='09X4n'></legend><bdo id='09X4n'><pre id='09X4n'><center id='09X4n'></center></pre></bdo></b><th id='09X4n'></th></span></q></dt></tr></i><div id='09X4n'><tfoot id='09X4n'></tfoot><dl id='09X4n'><fieldset id='09X4n'></fieldset></dl></div>