views:

269

answers:

6

When I use an initialization list:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

the primitive type (int, bool, float, enum, pointer) member variable is default-initialied. Is the value it gets implementation defined or is it the same for all implementations?

A: 

Native types like int usually get a garbage value, eg. whatever happens to reside in the memory area it is created in. However this is not defined in the standard, and it might also be initialized to 0, which is quite common in eg. debug builds.

EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.

reko_t
No, because the example in the question contains `memberVariable()`, which does initialise it.
Jon Hanna
That's true for C, but, as the standard says, not for C++.
Lagerbaer
Ah, you're right, I missed that.
reko_t
If it's explicitly *default initialized*, it will have a value.
You
+13  A: 

The Standard says (8.5/5)

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 Thas no accessible default constructor);

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

— otherwise, the object is zero-initialized.

.

To value-initialize an object of type T means:

— if Tis 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 Thas 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 Tis value-initialized;

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

— otherwise, the object is zero-initialized

.

Is the value it gets implementation defined or is it the same for all implementations?

So the value would be same for all implementations.

Struct is non-POD type so

 Struct *a =new Struct; // default initialization

 //memberVariable will be initialized to 0 because if T is a non-POD class type
 //the default constructor for T is called 

 Struct *b = new Struct(); //value initializes Struct, which calls the default ctor.

 //memberVariable will be initialized to 0 in this case also.

EDIT :

As @Johannes noticed the primitive type (int, bool, float, enum, pointer) member variable is value-initialized not default initialized.

Prasoon Saurav
Excellent answer. I especially like the reference to the standard. +1
JoshD
+6  A: 

For primitive types, default initialisation means that the object is initialised with 0, 0.0 or NULL as appropriate for the type.

Edit: The above is valid for C++98. In C++03, the terms got redefined a bit. Now, using an initialiser of () (which is syntactically only possible for member-objects) results in value initialisation, which for primitive types means that the appropriate value of 0, 0.0 or NULL gets stored.

Bart van Ingen Schenau
Nice, some new usenet guy in here :) Please notice this default-initialisation does not apply to his code (in C++03).
Johannes Schaub - litb
@Johannes:that is only because the definition of *default initialisation* got changed between C++98 and C++03. In C++03, the `memberVariable` is *value initialised*, with exactly the same effect.
Bart van Ingen Schenau
@Bart the definition of value initialization differs from the definition of default initialization. If `memerVariable` would be a non-POD without a user declared constructor, default initialization will not initialize its members, while value initialization *will* initialize them. Explaining how default initialization works makes people (including the questioner, who obviously does not know it) believe `memberVariable` is default-initialized, while really it is value initialized.
Johannes Schaub - litb
@Johannes: Please check the difference between the C++98 and C++03 standards in this respect. The terms have been redefined.
Bart van Ingen Schenau
@Bart we have had this kind of difference elaborated in length on SO. See http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializati . We have had many [questions and answers](http://stackoverflow.com/search?q=%22value+initialization%22) about this topic, [for example here](http://stackoverflow.com/questions/734958/c-empty-paren-member-initialization-zeroes-out-memory/735007#735007).
Johannes Schaub - litb
+2  A: 

0

If you call () on a primitive, the effect is the same as assigning the default value it would have been given if it had been static.

Jon Hanna
AFAIR, static variables have to be explicitly initialized. Am I mistaken?
DevSolar
@DevSolar, "Every object of static storage duration shall be zero-initialized at program startup before any other initialization takes place. [ Note: in some cases, additional initialization is done later. —end note ]" Section 8.5 of a draft copy of the standard I have, item 7.
Jon Hanna
A: 

It depends on how you instantiate a class, if you use ClassName() the POD classes are default initialized to zero for non POD class default constructor is called but if you use ClassName, without the parentheses no default initialization takes place.

yesraaj
-1. Sorry, but that rule only applies to classes with a compiler-supplied default constructor. Since `Struct::Struct()` is declared, it will be used for both `new Struct` and `new Struct()`.
MSalters
+5  A: 

You are not correct. The object is not default-initialized but value-initialized. And its value is well-defined

int = 0, 
bool = false, 
float = 0.0f,  
enum = (enum type)0, 
pointer = null pointer
pointer to member = null member pointer

Note that zero is in the range of values for any enumeration, even if it doesn't contain an explicit enumerator with that vaue, so it's safe to initialize an enumeration variable to that value.

In particular for pointer to data members, the representation used in practice is not all-zero bits. In the so-called C++ Itanium ABI used by at least GCC and Clang, pointer to data members have an all-one bits null representation.

Johannes Schaub - litb
Is the object value initialized (and **not** default initialized) because its a non-POD class type?
Prasoon Saurav
It's value initialized disregarding the type. `int` is not a non-POD class type.
Johannes Schaub - litb
@Johannes : No I was talking about `Struct` as a whole which is a non-POD type of-course. The subobjects are value initialized.
Prasoon Saurav
Is pointer to member type a primitive type?
Chubsdad
@Chubsdad : I have read somewhere that `C++TC1` made explicit that pointers to member are PODs.
Prasoon Saurav