Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zero initialization and static initialization of local scope static variable

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:

  • The order of initialization of C++ is:
    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):

  • 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();

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:
If you think my question is rather tedious, you can help explain your ideas based on the code above.

like image 258
Zachary Avatar asked Jul 23 '13 03:07

Zachary


People also ask

Are static variables initialized to zero?

If you do not explicitly initialize a static (or external) variable, it will have a value of zero of the appropriate type, unless it is a pointer, in which case it will be initialized to NULL .

Why are static variables initialized to zero?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .

Are local variables initialized to 0?

Local variables, just like instance fields, are initialized to 0 behind the scenes, by default.

Is initialisation mandatory for local static variable?

Initialization of a static variable is not mandatory. Its default value is 0.


1 Answers

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:

  • The order of initialization of C++ is:
    1. Zero Initialization;
    2. Static Initialization;
    3. Dynamic Initialization.

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

  • Zero Initialization: the memory is filled with 0s at the byte level.
  • Constant Initialization: a pre-computed (compile-time) byte pattern is copied at the memory location of the object
  • Static Initialization: Zero Initialization followed by Constant Initialization
  • Dynamic Initialization: a function is executed to initialize the memory

A simple example:

int const i = 5;     // constant initialization
int const j = foo(); // dynamic initialization
  • Static objects (variables included) are first Zero-initialized, and then Static-initialized.

Yes and no.

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

  • constant initialized if possible
  • dynamically initialized otherwise (the compiler could not compute the memory content at compile-time)

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):

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

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.

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.

  • 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.

  • 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.

  • 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();

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).

EDIT: 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.

like image 169
Matthieu M. Avatar answered Nov 15 '22 18:11

Matthieu M.