Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are C++ implicit types initialized to 0?

I grew some doubts after discussing this with colleagues...

As the title asks, when can it be assumed that built-in types will be initialized to 0 instead of an unknown value?

Do the rules vary among the c++ standards?

like image 989
Drew Dormann Avatar asked Feb 08 '13 13:02

Drew Dormann


People also ask

Does C initialize variables to 0?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address!

Which variables are automatically initialized to zero?

3) Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage.

What is implicit initialization?

Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.

Which variable is initialized to zero by compiler?

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 .


3 Answers

The full rules are in [dcl.init] (C++11). To summarize: when no initialiser is provided in a declaration, the entity is so-called default-initialised. For class types, this means the default constructor is called. For non-class types, this means no initialisation is performed.

However, [dcl.init] §9 states: "Every object of static storage duration is zero-initialized at program startup before any other initialization takes place."

This means that static-duration variables (such as namespace-scope variables) of non-class type are zero-initialised. Other objects of non-class types (such as local variables) are not initialised.

like image 109
Angew is no longer proud of SO Avatar answered Oct 24 '22 15:10

Angew is no longer proud of SO


Rather than answering the exact question you post, which has been answered before: only POD objects with static storage will be initialized to 0 automatically, I will try to provide pieces of code to get the compiler to initialize the members for you:

struct POD {
   int    a;
   double b; //...
};
// namespace level:
POD p;
void f() {
   POD n1;                  // uninitialized
   POD p1 = {};
   POD p2 = POD();
   POD* n2 = new POD;       // uninitialized
   POD* pp1 = new POD();
   delete n2; delete pp1;
}

In the examples above only those marked with 'uninitialized' won't be initiazed. Note that this is with respect to what the standard mandates, your mileage will vary with different compilers. In particular VS has some issues with T t = T(); and T* p = new T()' in some scenarios (IIRC when the type T is not a POD, but does not have a user provided default constructor the compiler will fail to initialize the POD subobjects:

struct T {
   std::string s;
   int         i;
};
void f() {
   T t = T();    // t.i == 0 according to the standard
}
like image 43
David Rodríguez - dribeas Avatar answered Oct 24 '22 14:10

David Rodríguez - dribeas


Theory:

According to both C++98 and C++03 standards:

3.6.2 Initialization of non-local objects, §1:

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.

3.7.1 Static storage duration, §1:

All objects which neither have dynamic storage duration nor are local have static storage duration.

3.7.1 Static storage duration, §3:

The keyword static can be used to declare a local variable with static storage duration.

And also 8.5 Initializers, §6:

Every object of static storage duration shall be zero-initialized at program startup before any other initialization takes place.

This is the same in both standards. The only difference is in formulation of C++98's 8.5 §6:

The memory occupied by any object of static storage duration shall be zero-initialized at program startup before any other initialization takes place.


Example:

Here's the example, where x and y and have static storage duration, thus standard guarantees that both of them will be zero-initialized at program startup. Note that there are also POD objects a and b declared in the same manner, thus having static storage duration, which means that their members (i and d) will be zero-initialized as well:

struct POD {
    int i;
    double d;
};

int x;
POD a;

int foo() {
    static int y;
    return y;
}

int main() {
    static POD b;
    std::cout << "x   = " << x      << std::endl;
    std::cout << "y   = " << foo()  << std::endl;
    std::cout << "a.i = " << a.i    << std::endl;
    std::cout << "b.d = " << b.d    << std::endl;
}

Output of this example is then of course:

x   = 0
y   = 0
a.i = 0
b.d = 0
like image 3
LihO Avatar answered Oct 24 '22 14:10

LihO