Dumb question, but whenever you call new, do you always have a pointer?
SomeClass *person = new SomeClass();
And is that because you need a pointer to point to that new space of memory that was allocated for the SomeClass variable person? Thanks!
new operator. The new operator denotes a request for memory allocation on the Free Store. If sufficient memory is available, a new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
An Example of allocating array elements using “new” operator is given below: int* myarray = NULL; myarray = new int[10]; Here, new operator allocates 10 continuous elements of type integer to the pointer variable myarray and returns the pointer to the first element of myarray.
Operator new returns a pointer to a beginning of an array. Pointer is the address of some cell in memory. that's why we can say: Operator new returns a address to a beginning of an array.
The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
If new
completes successfully, it always returns a pointer (if it doesn't complete successfully, an exception is thrown, and nothing is returned).
The pointer is to the object that was created, or in the case of an array, a pointer to the first element of the array.
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