Open In App
Related Articles

Zero Initialization in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Setting the initial value of an object to zero is called zero initialization.

Syntax

static T object;

Tt = {} ;

T {} ;

char array [n] = " ";

Zero initialization is performed in the following situations:- 

  1. Zero is initialized for every named variable with static or thread-local storage duration that is not subject to constant initialization (since C++14), before any other initialization.
  2. Zero is initialized as part of the value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors.
  3. When a character array is initialized with a string literal which is very short, the remainder of the array is zero-initialized.

The effects of zero-initialization are:  

  • If T is a scalar type, the object is initialized to the value obtained by converting the integer literal 0 to T.
  • If T is a non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits.
  • If T is a union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits.
  • If T is an array type, each array element is zero-initialized.
  • If T is a reference type, no initialization is performed.

Key Points:  

  • The static and thread-local variables are first zero-initialized and then initialized again as specified in the program, e.g. in the starting of a program, function-local static is first zero-initialized, and then its constructor is called when the function is first entered. If there is no initializer for the declaration of a non-class static, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.
  • A pointer which is zero-initialized is called a null pointer, even if the value of the null pointer is not integral zero.

Below program illustrates zero initialization in C++:  

CPP

// C++ code to demonstrate zero initialization
 
#include <iostream>
#include <string>
 
struct foo {
    int x, y, z;
};
 
double f[3]; // zero-initialized to three 0.0's
 
int* p; // zero-initialized to null pointer value
 
// zero-initialized to indeterminate value
// then default-initialized to ""
std::string s;
 
int main(int argc, char* argv[])
{
    foo x = foo();
     
    std::cout << x.x << x.y << x.z << '\n';
     
    return 0;
}

                    

Output
000

In this example logic is same but program is quite different than above example. Below is another method of Zero Initialization in C++.

C++

#include <iostream>
#include <string>
 
using namespace std;
 
struct S {
    int g, q, r;
};
double f[3];    //zero-initialised to three 0.0's
int* p;         // zero initialized to null pointer value.
string u;      
 
int main(int argc, char*[])
{
    delete p;      // safe to delete a null pointer
    static int n = argc;   //zero-initialized to 0then copy-initialized to argc
    cout << "n=" << n << '\n';
 
    S g = S();      //the effect is same as: S g{}; or Sg={};
    cout << "g={" << g.g <<" " << g.q <<" " << g.r
         << "} \n";
 
        return 0;
}

                    

Output
n=1
g={0 0 0} 


Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads