Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'new operator' and 'operator new'?

Tags:

c++

People also ask

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.

What is the new operator in C++?

Use of the new operator signifies a request for the memory allocation on the heap. If the sufficient memory is available, it initializes the memory and returns its address to the pointer variable. The new operator should only be used if the data object should remain in memory until delete is called.

What is difference between new and delete operators?

The main difference between new and delete operator in C++ is that new is used to allocate memory for an object or an array while, delete is used to deallocate the memory allocated using the new operator. There are two types of memory as static and dynamic memory.

What is the example of new operator?

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.


I usually try to phrase things differently to differentiate between the two a bit better, but it's a good question in any case.

Operator new is a function that allocates raw memory -- at least conceptually, it's not much different from malloc(). Though it's fairly unusual unless you're writing something like your own container, you can call operator new directly, like:

char *x = static_cast<char *>(operator new(100));

It's also possible to overload operator new either globally, or for a specific class. IIRC, the signature is:

void *operator new(size_t);

Of course, if you overload an operator new (either global or for a class), you'll also want/need to overload the matching operator delete as well. For what it's worth, there's also a separate operator new[] that's used to allocate memory for arrays -- but you're almost certainly better off ignoring that whole mess completely.

The new operator is what you normally use to create an object from the free store:

my_class *x = new my_class(0);

The difference between the two is that operator new just allocates raw memory, nothing else. The new operator starts by using operator new to allocate memory, but then it invokes the constructor for the right type of object, so the result is a real live object created in that memory. If that object contains any other objects (either embedded or as base classes) those constructors as invoked as well.


"operator new"

class Foo
{
public:
        void* operator new( size_t );
}

"new operator":

Foo* foo = new Foo();

In this example, new Foo() calls Foo::operator new()

In other words, "new operator" calls "operator new()" just like the + operator calls operator +()


Following is the quote from More Effective C++ book from Scott Meyers:

The new operator calls a function to perform the requisite memory allocation, and you can rewrite or overload that function to change its behavior. The name of the function the new operator calls to allocate memory is operator new.


There's no difference between "new operator" and "operator new". Both refer to the same thing: the overloadable/replaceable operator new function that typically performs raw memory allocation for objects created by new-expressions.

Note also that neither term is present in the language specification (which is the defining source of the official terminology).

When you use new in your program to create an object, it is called new-expression. New-expression consists of keyword new and additional syntactic parts defined by the grammar. No part of this expression's syntax is ever referred to as an "operator".

The raw memory allocation function operator new is officially referred to as just "operator new function". Note that the words operator and new in this sequence are just two separate C++ language keywords. They don't form an English term "operator new". Nowhere in the language specification you'll find any references to "operator new" as an English term. Every time this is just a combination of two independent keywords that produce declaration syntax for a memory allocation function.

Again, in resume: formally in C++ there's no such English language terms as "operator new" or "new operator". The former sequence is present in the language specification as a mere combination of keywords, not as an English term. The latter is not present at all.


When you create a new object the memory is allocated using operator new then the constructor is invoked to initialise the memory. The new operator does both the allocation and the initialisation, where as the operator new only does the allocation.