Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/zero initialization"

From cppreference.com
< cpp‎ | language
m (Use new list syntax)
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
Line 61: Line 61:
 
* [[cpp/language/copy_initialization|copy initialization]]
 
* [[cpp/language/copy_initialization|copy initialization]]
 
* [[cpp/language/list_initialization|list initialization]]
 
* [[cpp/language/list_initialization|list initialization]]
 +
 +
[[de:cpp/language/zero initialization]]
 +
[[es:cpp/language/zero initialization]]
 +
[[fr:cpp/language/zero initialization]]
 +
[[it:cpp/language/zero initialization]]
 +
[[ja:cpp/language/zero initialization]]
 +
[[pt:cpp/language/zero initialization]]
 +
[[ru:cpp/language/zero initialization]]
 +
[[zh:cpp/language/zero initialization]]

Revision as of 20:01, 2 November 2012

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

Sets the initial value of an object to zero

Contents

Syntax

static Template:sparam Template:sparam ; (1)
Template:sparam () ; (2)
char Template:sparam [ Template:sparam ] = ""; (3)

Explanation

Zero initialization is performed in the following situations:

1) For every named variable with static or thread-local storage duration, before any other initialization.
2) As part of 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 that is too short, the remainder of the array is zero-initialized.

The effects of zero initialization are:

  • If T is a scalar type, the object's initial value is the integral constant zero implicitly converted to T.
  • 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

The static and thread-local variables are first zero-initialized and then initialized again as specified in the program, e.g. a function-local static is first zero-initialized at program startup, and then its constructor is called when the function is first entered. If the declaration of a non-class static 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
}

See also