zero initialization
Sets the initial value of an object to zero
| Contents | 
[edit] Syntax
| staticT object; | (1) | ||||||||
| int (); | (2) | ||||||||
| chararray[n]= ""; | (3) | ||||||||
[edit] Explanation
Zero initialization is performed in the following situations:
The effects of zero initialization are:
-  If Tis a scalar type, the object's initial value is the integral constant zero explicitly converted toT.
-  If Tis an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.
-  If Tis a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits.
-  If Tis array type, each element is zero-initialized
-  If Tis reference type, nothing is done.
[edit] Notes
As described in non-local initialization, static and thread-local variables that aren't constant-initialized (since C++14) 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.
[edit] Example
#include <string> double f[3]; // zero-initialized to three 0.0's int* p; // zero-initialized to null pointer value std::string s; // zero-initialized to indeterminate value // then default-initialized to "" int main(int argc, char* argv[]) { static int n = argc; // zero-initialized to 0 // then copy-initialized to argc delete p; // safe to delete a null pointer }
 
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior | 
|---|---|---|---|
| CWG 2026 | C++14 | zero-init was specified to always occur first, even before constant-init | no zero-init if constant init applies |