Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the "new" operator in c++, pointer question

Tags:

c++

pointers

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!

like image 618
Crystal Avatar asked Apr 28 '10 01:04

Crystal


People also ask

What is new operator in C?

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.

What is new operator in C give example?

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.

Does the new operator return a pointer?

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.

What is the new operator?

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.


1 Answers

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.

like image 165
James McNellis Avatar answered Oct 17 '22 09:10

James McNellis