what's the difference between
int * num = new (int);
and
int * num = new int;
?
Is there a difference at all?
EDIT thx all. ... which one is the most correct answer?
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).
*new int means "allocate memory for an int , resulting in a pointer to that memory, then dereference the pointer, yielding the (uninitialized) int itself".
int *array = new int[n]; It declares a pointer to a dynamic array of type int and size n . A little more detailed answer: new allocates memory of size equal to sizeof(int) * n bytes and return the memory which is stored by the variable array .
int * means a pointer to an integer in your memory. The [] bracket stands for an array. int a[10]; would make an array of 10 integers. int *a; would make a pointer to an integer.
There isn't a difference in the context of your example (using an int type). However, there is a difference if you need to create objects of compound types, where you need to use parenthesized version. i.e:
int (**fn_ptr_ok) () = new (int (*[10]) ()); // OK int (**fn_ptr_err) () = new int (*[10]) (); // error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With