What is the difference between operator new and the new operator?

Showing Answers 1 - 6 of 6 Answers

This is what happens when you create a new object: 1. the memory for the object is allocated using "operator new". 2. the costructor of the class is invoked to properly initialize this memory. As you can see, the new operator does both 1 and 2. The operator new merely allocates memory, it does not initialize it. where as the new operator also initializes it properly by calling the constructor.

vsvraju

  • Aug 8th, 2008
 

Operator new mean overloading the operator 'new', giving your own defintion for the existing 'new' operator.

  Was this answer useful?  Yes

'new' operator allocates new instance of object from the heap, thus utilising the most appropiate constructor for the argument.

Operator new is the mechanism of overriding the default heap allocation logic.

  Was this answer useful?  Yes

satheesh83

  • Oct 16th, 2009
 

New operator allows to allocate a memory from the heap, so a new instance of
a class is created but operator New is used to overload the (new) operator just
like overloading of other operators.


Additionally:
The term "operator new" is used incase when u are overloading the global "new"
operator. We can overload new operator just as any other operators i.e
+,-,*,=etc.
The term "new operator" is used in case of dynamic allocation of memory. When a
variable is allocated memory dynamically the new operator is used.


Eg:
int *p=new int;
//here p is pointer to integer. So in order to allocate memory for it we have
used the new operator.

  Was this answer useful?  Yes

Operator new works like malloc.

Example:
int *n; n = (int*) operator new(sizeof(int));

New Operator does the following:
1) Calculate the memory
2) Allocate memory using operator new
3) Call the Constructor

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions