局部范围静态变量的零初始化和静态初始化 [英] zero initialization and static initialization of local scope static variable

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

问题描述

我在Google上查看了有关 C ++初始化的几个帖子,其中一些帖子在StackOverflow上指向我。我从这些帖子中挑选的概念如下:

I read several posts on C++ initialization from Google, some of which direct me here on StackOverflow. The concepts I picked from those posts are as follows:


  • C ++初始化的顺序是:

  1. 零初始化;

  2. 静态初始化;

  3. 动态初始化

  1. Zero Initialization;
  2. Static Initialization;
  3. Dynamic Initialization.


  • 首先是已初始化,然后是静态初始化

  • Static objects (variables included) are first Zero-initialized, and then Static-initialized.
  • 有关初始化问题(存储类问题也可能有相关问题)的几个查询:

    I have several inquiries as to the initialization issue (storage class issue may be related as well):


    • 全局对象(未定义静态关键字)也是静态对象?

    • 也通过上述两个步骤以静态对象进行初始化,对吗?

    • 什么是静态初始化?它是指初始化静态对象(用 static 关键字定义)?

    • 我也读取在块内定义的对象static 关键字在执行线程首次进入块时被初始化!这意味着本地静态对象 main 函数执行之前未初始化。这意味着它们不会被初始化为上述两个步骤,对吗?

    • 动态初始化指的是由操作员,对吧?它可能引用像 myClass obj = myClass(100); myClass obj = foo(); li>
    • Global objects (defined without static keyword) are also static objects, right?
    • Global objects are also initialized like static objects by two steps like above, right?
    • What is the Static Initialization? Does it refer to initializing static objects (defined with static keyword)?
    • I also read that objects defined within block (i.e. in a function) with static keyword is initialized when the execution thread first enters the block! This means that local static objects are not initialized before main function execution. This means they are not initialized as the two steps mentioned above, right?
    • Dynamic initialization refers to initialization of objects created by new operator, right? It might refer to initialization like myClass obj = myClass(100); or myClass obj = foo();

    我对初始化和存储类说明符问题的查询过多。我读了C ++ 2003标准文档,但是找不到一个明确的逻辑,因为它们分散在整个文档。

    I have too many inquiries on the initialization and storage class specifier issues. I read the C++2003 Standard document, but cannot find a clear logic since they are scattered throughout the document.

    我希望你给我一个答案,逻辑解释存储类说明符和初始化的整个映射。欢迎任何参考!

    I hope you give me an answer that logically explains the whole map of storage class specifier and initialization. Any reference is welcome!

    可能解释我的问题的代码:

    Code that might explain my question:

    class myClass{
    public:
       int i;
       myClass(int j = 10): j(i){}
       // other declarations
    };
    
    myClass obj1;//global scope
    static myClass obj2(2);//file scope
    {   //local scope
       myClass obj3(3);
       static myClass obj4(4);
    }
    






    > EDIT

    如果您认为我的问题相当乏味,您可以根据上述代码来说明您的想法。


    EDIT:
    If you think my question is rather tedious, you can help explain your ideas based on the code above.

    推荐答案


    我在Google上阅读了几个关于 C ++初始化的帖子,其中一些在StackOverflow上指向我。我从这些帖子中选择的概念如下:

    I read several posts on C++ initialization from Google, some of which direct me here on StackOverflow. The concepts I picked from those posts are as follows:


    • C ++初始化的顺序是:

    1. 零初始化;

    2. 静态初始化;

    3. 动态初始化

    1. Zero Initialization;
    2. Static Initialization;
    3. Dynamic Initialization.


    是的,确实有3个阶段(在标准中)。让我们在继续之前澄清它们:

    Yes, indeed there are 3 phases (in the Standard). Let us clarify them before continuing:


    • 零初始化:内存在字节级填充0。

    • 常量初始化:在对象的内存位置复制预计算(编译时)字节模式

    • 静态初始化:零初始化, >
    • 动态初始化:执行函数以初始化内存



    A simple example:

    int const i = 5;     // static initialization
    int const j = foo(); // dynamic initialization
    





    • 静态对象(包括变量)首先是零初始化,然后是静态初始化

    • Static objects (variables included) are first Zero-initialized, and then Static-initialized.

    是和否。

    标准要求对象首先进行零初始化,是:

    The Standard mandates that the objects be first zero-initialized and then they are:


    • 如果可能,常量将初始化

    • 否则动态初始化(编译器无法计算内存内容)

    注意:在常量初始化的情况下,编译器可能会忽略第一个零初始化的内存

    Note: in case of constant initialization, the compiler might omit to first zero-initialized memory following the as-if rule.


    我有几个关于初始化问题的查询存储类问题也可能相关):

    I have several inquiries as to the initialization issue (storage class issue may be related as well):


    • 全局对象<没有 static 关键字)也是静态对象,是吗?

    • Global objects (defined without static keyword) are also static objects, right?

    ,在文件范围 static 对象只是符号的可见性。全局对象可以通过名称从另一个源文件引用,而 static 对象名称完全在当前源文件的本地。

    Yes, at file scope the static object is just about the visibility of the symbol. A global object can be referred to, by name, from another source file whilst a static object name is completely local to the current source file.

    在许多不同的情况下,混淆起源于世界的重用 static :(

    The confusion stems from the reuse of the world static in many different situations :(



    • 全局对象也按照上述两个步骤初始化为静态对象,对吗?

    • Global objects are also initialized like static objects by two steps like above, right?

    是的,实际上是本地静态对象。

    Yes, as are local static objects in fact.


    • 静态初始化是指初始化静态对象(使用 static 关键字定义) / li>
    • What is the Static Initialization? Does it refer to initializing static objects (defined with static keyword)?

    不是,如上所述,它指的是初始化对象而不执行用户定义的函数,注意,对于稍后将动态初始化的对象,这只是将内存置零。

    No, as explained above it refers to initializing objects without executing a user-defined function but instead copying a pre-computed byte pattern over the object's memory. Note that in the case of objects that will later be dynamically initialized, this is just zero-ing the memory.



    • 我还读取了块中定义的对象(即在函数中)与 static 关键字初始化时执行线程首先进入块!这意味着本地静态对象 main 函数执行之前未初始化。这意味着它们没有被初始化为上述两个步骤,对吧?

    • I also read that objects defined within block (i.e. in a function) with static keyword is initialized when the execution thread first enters the block! This means that local static objects are not initialized before main function execution. This means they are not initialized as the two steps mentioned above, right?

    这两个步骤的过程,虽然的确只有第一次执行通过他们的定义。

    They are initialized with the two steps process, though indeed only the first time execution pass through their definition. So the process is the same but the timing is subtly different.

    在实践中,如果他们的初始化是静态的(即,内存模式是一个编译时模式),那么这个过程是一样的,

    In practice though, if their initialization is static (ie, the memory pattern is a compile-time pattern) and their address is not taken they might be optimized away.

    请注意,在动态初始化的情况下,如果它们的初始化失败(由函数抛出的异常应该初始化它们将在下一次流控制通过其定义时重新尝试。

    Note that in case of dynamic initialization, if their initialization fails (an exception is thrown by the function supposed to initialize them) it will be re-attempted the next time flow-control passes through their definition.



    • 动态初始化是指由运算符创建的对象的初始化,对吗?它可能引用像 myClass obj = myClass(100); myClass obj = foo(); li>
    • Dynamic initialization refers to initialization of objects created by new operator, right? It might refer to initialization like myClass obj = myClass(100); or myClass obj = foo();

    不指定初始化,需要执行用户定义的函数c $ c> std :: string 对于C ++语言有一个用户定义的构造函数。)

    Not at all, it refers to initialization requiring the execution of a user defined function (note: std::string has a user-defined constructor as far as the C++ language is concerned).

    我感谢Zach指向我,我错误地称为静态初始化什么C ++ 11标准调用常量初始化;此错误现在应该是固定的。

    My thanks to Zach who pointed to me I erroneously called Static Initialization what the C++11 Standard calls Constant Initialization; this error should now be fixed.

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

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