I just need to understand what the differences between these are:
int *p = new int[5];
and
int *p = new int(5);
I just need to understand what the differences between these are:
int *p = new int[5];
and
int *p = new int(5);
One creates an array of five ints and assigns a pointer to this array's first element to p. None of the integers in this array are initialized:
int *p = new int[5]; // a pointer to an array of 5 integers
The other one creates a single int, and assigns a pointer to that int to p. This integer is initialized with 5:
int *p = new int(5); // a pointer to an int
As tadman points out in his comment, in essence, the difference is between the operator new and operator new[]. new[] allocates an array and must be deleted with delete[], while new allocates a single object and must be deleted with delete. If you delete with the wrong operator, what happens is undefined behavior. Of course, unless you are a library implementer, you should generally prefer smart pointers and library containers over new and new[].