Why is a new keyword return pointer referenced instead of a link?

I know about a newkeyword that returns a pointer, so the return type void*. My stupid question is why newshould return void*instead void&? When an object created with newcan return the address of that object using the address of the operator &.

I know the difference between a pointer and a link. But in the end, we are working with a pointer instead of a link. Please clear me of the confusion.

+4
source share
2 answers

The new one returns a pointer of any type that you used to allocate space in memory, not void *

 auto p1 = new int[5];   // returns a pointer to an int pointing to the 1st element
 auto p2 = new short[5]; // returns a pointer to a short pointing to the 1st element

, . . .

delete[] p1;
p1 = new int[20]; 
+4

, new , , , , ++ C-, NULL . , null .

new , , , . non const lvalue .

+3

All Articles