What is the difference between following two lines ?
int *a = new int;
int *a = new int();
int *a = new int;
a
is pointing to default-initialized object (which is uninitialized object in this case i.e the value is indeterminate as per the Standard).
int *a = new int();
a
is pointing to value-initialized object (which is zero-initialized object in this case i.e the value is zero as per the Standard).
The first variant default-initializes the dynamically allocated int
, which for built-in types such as int
does not perform any initialization.
The second variant value-initializes it, which for int
means zero-initialization, giving it value 0
.