[Solved] what is the different between ‘new()’ int and ‘int *p’ [closed]


No. They are far from the same.

This:

int *p;
*p = 10;

Declares a (uninitialized) pointer to int named p. Then assignes the value 10 to what p points to.
This is ” undefined behaviour” since p is uninitialized.

This:

int *q = new() int(10);

Declares a int pointer q and initializes it to point to the storage allocated by new which has allocated space for 10 int’s (if we disregard the syntactically incorrect “()” after new).

To summarize: both – as written – are invalid, broken code, but not the same.

Note: new(...) is “placement new” which is not what you want here.

solved what is the different between ‘new()’ int and ‘int *p’ [closed]