Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are primitive types default-initialized to in C++?

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?

like image 696
sharptooth Avatar asked Sep 27 '10 11:09

sharptooth


People also ask

What is the default value of INT in C?

The default value of Integer is 0.

What are pointers default initialized to?

For the sake of sanity, always initialize pointers to NULL, in that way if any attempt to dereference it without malloc or new will clue the programmer into the reason why the program mis-behaved.


2 Answers

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.

like image 158
Johannes Schaub - litb Avatar answered Sep 30 '22 23:09

Johannes Schaub - litb


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 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

.

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 a 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.

like image 33
Prasoon Saurav Avatar answered Sep 30 '22 22:09

Prasoon Saurav