Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference in c++ between new int and new (int)?

Tags:

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?

like image 285
groovehunter Avatar asked Mar 18 '11 09:03

groovehunter


People also ask

What is difference between new int and 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).

What does new int do in C?

*new int means "allocate memory for an int , resulting in a pointer to that memory, then dereference the pointer, yielding the (uninitialized) int itself".

What does new int *[ N mean?

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 .

Is int * and int * the same?

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.


1 Answers

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  
like image 153
sinek Avatar answered Jan 03 '23 02:01

sinek