What Does 'Value Initializing' Something Mean

What does 'value initializing' something mean?

A declared variable can be Zero Initialized, Value Initialized or Default Initialized.

In your example:

Info *p = new Info();    <------- Value Initialization
Info *p = new Info; <------- Default Initialization

The C++03 Standard 8.5/5 aptly defines each:

To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

— if T is a non-union class type, each nonstatic data member and each base-class subobject

is zero-initialized;

— if T is a union type, the object’s first named data member is zero-initialized;

— if T is an array type, each element is zero-initialized;

— if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:

— if T is a non-POD class type (clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

— otherwise, the object is zero-initialized.

To value-initialize an object of type T means:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default
constructor for T is called (and the initialization is ill-formed if T has no accessible
default constructor);

— if T is a non-union class type without a user-declared constructor, then every non-static
data member and base-class component of T is value-initialized;

— if T is an array type, then each element is value-initialized;

— otherwise, the object is zero-initialized

Difference between default-initialize and value-initialize?

According to the standard (8.5/4,5):

To default-initialize an object of
type T means:

— if T is a non-POD
class type the default constructor for
T is called (and the initialization is
ill-formed if T has no accessible
default constructor);

— if T is an
array type, each element is
default-initialized;

— otherwise, the
object is zero-initialized.


To value-initialize an object of
type T means:

— if T is a class type
(clause 9) with a user-declared
constructor (12.1), then the default
constructor for T is called (and the
initialization is ill-formed if T has
no accessible default constructor);


if T is a non-union class type without
a user-declared constructor, then
every non-static data member and
base-class component of T is
value-initialized;96)
— if T is an
array type, then each element is
value-initialized;

— otherwise, the
object is zero-initialized

Why is value initialization so named?

The Boost value_init write-up provide a rather detailed history of value initialization it ended up in the standard from defect report 178: More on value-initialization and it seems like the term originated from defect report 35: Definition of default-initialization. Although none of these documents really provide a proper origin for the term it does provide some good ideas, it says:

The first Technical Corrigendum for the C++ Standard (TC1), whose
draft was released to the public in November 2001, introduced Core
Issue 178 (among many other issues, of course).

That issue introduced the new concept of value-initialization (it also
fixed the wording for zero-initialization). Informally,
value-initialization is similar to default-initialization with the
exception that in some cases non-static data members and base class
sub-objects are also value-initialized. The difference is that an
object that is value-initialized won't have (or at least is less
likely to have) indeterminate values for data members and base class
sub-objects; unlike the case of an object default constructed. (see
Core Issue 178 for a normative description).

In order to specify value-initialization of an object we need to use
the empty-set initializer: ().

and value initialization is less likely to leave an object with an indeterminate value versus default-initalization.

Meaning of default init and value init?

int i; // does this make i uninitialized?

Yes, if in local scope and not in global scope.

int i{}; // does this make i = 0?

Yes, always.

std::array<int, 3> a; // is a all zeros or all uninitialized?

Uninitialized if in a local scope, but zeroed of in global scope, i.e., same as your first question.

std::array<int, 3> a{}; // same as above?

All values are default initizlized, i.e., all three elements are zeroed.

Why does initialization happen in the following case?

The temporary object needs to be initialized, so that y can be assigned its value.

Value initialization on explicit constructor call in C++?

Firstly, what happens actually if no constructor is called

A constructor for a class-type is always called when an object is constructed, be it user-defined or compiler-generated. The object is initialized, but the members can remain un-initialized. This makes the second part of the question obsolete.

Second, is there documentation that supports/mentions/explains this behaviour ?

The all-mighty standard.

Meaning of default initialization changed in C++11?

The final effects are almost the same. In C++03, the use of default-initialize was restricted to non-POD class type, so the last point never applied. In C++11, the standard simplifies the wording by eliminating the condition with regards to where default-initialization was used, and changes the definition of default-initialization to cover all of the cases in a way to correspond what happened before.

What does 'initialization' exactly mean?

It means exactly what it says. Initialized static storage duration objects will have their init values set before the main function is called. Not initialized will be zeroed. The second part of the statement is actually implementation dependant, and implementation has the full freedom of the way it will be archived.

When you declare the variable without the keyword extern you always define it as well



Related Topics



Leave a reply



Submit