Difference between revisions of "cpp/language/zero initialization"
(link to non-local initialization for the more complete description) |
(cwg 2026 (no zero-init before constant-init)) |
||
Line 14: | Line 14: | ||
Zero initialization is performed in the following situations: | Zero initialization is performed in the following situations: | ||
− | @1@ For every named variable with static or thread-local [[cpp/language/storage_duration|storage duration]], before any other initialization. | + | @1@ For every named variable with static or thread-local [[cpp/language/storage_duration|storage duration]] {{rev inl|since=c++14|that is not subject to {{rlp|constant initialization}}}}, before any other initialization. |
@2@ As part of [[cpp/language/value_initialization|value-initialization]] sequence for non-class types and for members of value-initialized class types that have no constructors. | @2@ As part of [[cpp/language/value_initialization|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 {{rlp|aggregate initialization#Character arrays|initialized with a string literal}} that is too short, the remainder of the array is zero-initialized. | @3@ When a character array is {{rlp|aggregate initialization#Character arrays|initialized with a string literal}} that is too short, the remainder of the array is zero-initialized. | ||
Line 31: | Line 31: | ||
===Notes=== | ===Notes=== | ||
− | As described in [[cpp/language/initialization#Non-local_variables|non-local initialization]], static and thread-local variables 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. | + | As described in [[cpp/language/initialization#Non-local_variables|non-local initialization]], static and thread-local variables {{rev inl|since=c++14|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. | A zero-initialized pointer is the null pointer value of its type, even if the value of the null pointer is not integral zero. | ||
Line 54: | Line 54: | ||
| output= | | output= | ||
}} | }} | ||
+ | |||
+ | ===Defect reports=== | ||
+ | {{dr list begin}} | ||
+ | {{dr list item|wg=cwg|dr=2026|std=C++14|before=zero-init was specified to always occur first, even before constant-init|after=no zero-init if constant init applies}} | ||
+ | {{dr list end}} | ||
===See also=== | ===See also=== |
Revision as of 06:55, 5 November 2015
Sets the initial value of an object to zero
Contents |
Syntax
static T object ;
|
(1) | ||||||||
int () ;
|
(2) | ||||||||
char array [ n ] = "";
|
(3) | ||||||||
Explanation
Zero initialization is performed in the following situations:
The effects of zero initialization are:
- If
T
is a scalar type, the object's initial value is the integral constant zero explicitly converted toT
.
- If
T
is 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
T
is a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits.
- If
T
is array type, each element is zero-initialized
- If
T
is reference type, nothing is done.
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.
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 }
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 |