What is the difference between "int * a = new int" and "int * a = new int ()"?

What is the difference between two lines?

int *a = new int; int *a = new int(); 
+7
c ++
source share
2 answers
 int *a = new int; 

a indicates the default initialized object (which is an uninitialized object in this case, i.e. the value is undefined according to the standard).

 int *a = new int(); 

a indicates a value-initialized object (which is a zero-initialized object in this case, i.e. the value is zero in accordance with the standard).

+11
source share

The first option, by default, initializes a dynamically allocated int , which for built-in types such as int does not perform any initialization.

The second option value - initializes it, which for int means zero initialization, giving it a value of 0 .

+5
source share

All Articles