27

Possible Duplicate:
What do the following phrases mean in C++: zero-, default- and value-initialization?

If I have a class for example:

class Info
{
   int x;
   int y;
};

which I used to created an object,

Info *p = new Info();

Does the brackets beside Info mean i'm value initializing it? How does it different from this, Info *p = new Info; ?

I know there is a question which differentiate between different meanings in new and old C++ language but I want to know the semantic difference between default and value initialization e.g. Does value initialization means initializing something to zero?

Community
  • 1
  • 1
user1086635
  • 1,536
  • 2
  • 15
  • 21
  • possible duplicate of [What do the following phrases mean in C++: zero-, default- and value-initialization?](http://stackoverflow.com/questions/1613341/), [Difference between default-initialize and value-initialize in C++03?](http://stackoverflow.com/questions/7084831/). – outis Jan 14 '12 at 07:21

1 Answers1

40

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • In my case, class `Info` is POD class type. Does it mean default initialization will be zero initialization? – user1086635 Jan 14 '12 at 07:38
  • @user1086635: Yes. It is clearly mentioned in the second paragraph of the Standard Quote above. – Alok Save Jan 14 '12 at 07:42
  • 1
    This answer is invalid for anything later than C++03. See the "Possible Duplicate" for an updated answer, as well as [cppreference](https://en.cppreference.com/w/cpp/language/default_initialization). – Lytigas Aug 02 '20 at 01:09
  • 2
    Clarification on default initialization: according to [cppreference](https://en.cppreference.com/w/cpp/language/default_initialization) the last bullet should be "otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values." This is what allows default initialized scalars to have any value rather than be zero initialized. – ryan0270 Nov 12 '20 at 15:43