Initializing default class data elements in C ++ 11

I am confused in the default initialization for class data members. Here is a sample code.

#include <iostream> #include <vector> class A { public: int i; A() {} }; A a1; A aa1[3]; std::vector<A> av1(3); int main() { A a2; A aa2[3]; std::vector<A> av2(3); std::cout << a1.i << " " << a2.i << std::endl; // 0 undefined std::cout << aa1[0].i << " " << aa2[0].i << std::endl; // 0 undefined std::cout << av1[0].i << " " << av2[0].i << std::endl; // undefined undefined } 

In the above code, only a1.i and aa1[0~2].i initialized to 0, while others are not initialized. I do not know why this happened.

To be specific, I already know that (from the "C ++ Primer"):

  • Initialization Process:

    • a1 and a2 initialized by default.
    • each element aa1 and aa2 initialized by default.
    • each element av1 and av2 initialized with a value.
  • The default initialization process:

    • check if the variable is a built-in type or a class type.
    • for the built-in type, if the variable is outside any function body, then it is initialized to 0, otherwise the value is undefined.
    • for a class type, if the class has a default value of ctor, then it is called, otherwise it is a compilation error.
  • The process of initializing the value:

    • check if the variable is a built-in type or a class type.
    • for an inline type, it is initialized to 0.
    • for a class type, it is initialized by default. (What I think means that if the class has a default value of ctor, then it is called, otherwise it is a compilation error.)

So, when ctor A::A() is called, how is the A::i data element initialized (I assume it is initialized by default)? And why only a1.i and aa1[0~2].i initialized equal to 0, while others are not initialized?

+7
c ++ c ++ 11
source share
3 answers

when ctor A :: A () is called, how is the data element A :: i initialized

If no initializer is provided, the default initialization rules apply. Your constructor does not initialize A::i , so it has remained uninitialized; this value is indefinite. No doubt about that. Excerpt from the default initialization documentation :

If T is a class type, the constructors are considered and resolved to overload with respect to the empty argument list. The selected constructor (which is one of the default constructors) is invoked to provide an initial value for the new object.


why only a1.i and aa1 [0 ~ 2] .i are initialized equal to 0, while others are not initialized?

The global data memory is initialized to zero, i.e. the entire section is zeroed, so the global A::i initialized to 0 . Note that the constructor will not do this. Excerpt from the documentation :

Static initialization

[...]

2) For all other nonlocal static and stream local variables, Zero initialization takes place. In practice, variables that will be initialized with zeros are placed in the .bss segment of the program image, which does not take up disk space and resets the OS when the program loads.

However, for vector , vector itself is in non-local static memory, and its elements are allocated in free storage (heap), and therefore their members are also not initialized.

+3
source share

You declare a constructor for A , so there is no default initialization i ; what is your responsibility in the constructor. Global variables get initial values โ€‹โ€‹of 0 because they are global. All global variables are initialized to 0 if in some way they are not assigned an initial value.

Local variables and vectors receive random data because it is either in memory on the stack (for local) or heap (for the allocated memory that the vector uses) that are used by these A instances.

+1
source share

Global (both static and exported) variables are zero-initialized (in practice, on ordinary platforms, they are in memory, which are filled with zeros when the program starts), unless they are explicitly initialized and have no constructor.

Vector elements are initialized by default when you use the constructor that you use in your code.

For an object of a vector class, both mean a constructor call. But for integers, the first means are initialized to zero, and the last means initialization with an undefined value. Thus, the simple int variables in your code are zero-initialized, but the ints in the vector are initialized by default.

Some links for reference:

+1
source share

All Articles