Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Zero-initialization

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      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
      Special member functions
      Templates
      Miscellaneous
       
       

      Sets the initial value of an object to zero.

      Contents

      [edit]Syntax

      Note that this is not the syntax for zero-initialization, which does not have a dedicated syntax in the language. These are examples of other types of initializations, which might perform zero-initialization.

      staticTobject; (1)
      T();

      Tt={};

      T{};(since C++11)

      (2)
      CharTarray[n]="short-sequence"; (3)

      [edit]Explanation

      Zero-initialization is performed in the following situations:

      1) For every named variable with static or thread-local(since C++11)storage duration that is not subject toconstant initialization, before any other initialization.
      2) As part ofvalue-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements ofaggregates for which no initializers are provided.
      3) When an array of anycharacter type isinitialized with a string literal that is too short, the remainder of the array is zero-initialized.

      The effects of zero-initialization are:

      • IfT is ascalar type, the object is initialized to the value obtained byexplicitly converting the integer literal0 (zero) toT.
      • IfT is a non-union class type:
      • allpadding bits are initialized to zero bits,
      • each non-staticdata member is zero-initialized,
      • each non-virtual base classsubobject is zero-initialized, and
      • if the object is not a base class subobject, eachvirtual base class subobject is zero-initialized.
      • IfT is a union type:
      • all padding bits are initialized to zero bits, and
      • the object’s first non-static named data member is zero-initialized.
      • IfT is array type, each element is zero-initialized.
      • IfT is reference type, nothing is done.

      [edit]Notes

      As described innon-local initialization, static and 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.

      [edit]Example

      Run this code
      #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 staticint n= argc;// zero-initialized to 0 then copy-initialized to argcstd::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";}

      Possible output:

      n = 1a = {0 0 0}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 277C++98pointers might be initialized with a non-constant
      expression of value 0, which is not a null pointer constant
      must initialize with an integral
      constant 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 value
      converted from an integral constant expression with value 0
      the object is initialized to the value
      converted from the integer literal0
      CWG 2026C++98zero-initialization was specified to always
      occur first, even before constant initialization
      no zero-initialization if
      constant initialization applies
      CWG 2196C++98zero-initialization for class types ignored base class subobjectsthey are also zero-initialized
      CWG 2253C++98it was unclear whether zero-initialization
      applies to unnamed bit-fields
      it applies (all padding bits
      are initialized to zero bits)

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/zero_initialization&oldid=151936"

      [8]ページ先頭

      ©2009-2025 Movatter.jp