Zero-initialization

Sets the initial value of an object to zero.

# Notes

As described in non-local initialization, staticand thread-local(since C++11) variables that aren’t constant-initialized are zero-initialized before any other initialization takes place. If the definition of a non-class non-local variable has no initializer, then default initialization does nothing, leaving the result of the earlier zero-initialization unmodified.

A zero-initialized pointer is the null pointer value of its type, even if the value of the null pointer is not integral zero.

# Example

#include <iostream>
#include <string>
 
struct A
{
    int a, b, c;
};
 
double f[3];   // zero-initialized to three 0.0's
 
int* p;        // zero-initialized to null pointer value
               // (even if the value is not integral 0)
 
std::string s; // zero-initialized to indeterminate value, then
               // default-initialized to "" by the std::string default constructor
 
int main(int argc, char*[])
{
    delete p; // safe to delete a null pointer
 
    static int n = argc; // zero-initialized to 0 then copy-initialized to argc
    std::cout << "n = " << n << '\n';
 
    A a = A(); // the effect is same as: A a{}; or A a = {};
    std::cout << "a = {" << a.a << ' ' << a.b << ' ' << a.c << "}\n";
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 277C++98pointers might be initialized with a non-constantexpression of value 0, which is not a null pointer constantmust initialize with an integralconstant expression of value 0
CWG 694C++98zero-initialization for class types ignored paddingpadding is initialized to zero bits
CWG 903C++98zero-initialization for scalar types set the initial value to the valueconverted from an integral constant expression with value 0the object is initialized to the valueconverted from the integer literal 0
CWG 2026C++98zero-initialization was specified to alwaysoccur first, even before constant initializationno zero-initialization ifconstant initialization applies
CWG 2196C++98zero-initialization for class types ignored base class subobjectsthey are also zero-initialized
CWG 2253C++98it was unclear whether zero-initializationapplies to unnamed bit-fieldsit applies (all padding bitsare initialized to zero bits)

# See also