Difference Between 'New Operator' and 'Operator New'

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

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.

Difference between new operator in C++ and new operator in java

  • In C++, T * p = new T;...
  1. allocates enough memory for an object of type T,

  2. constructs an object of type T in that memory, possibly initializing it, and

  3. returns a pointer to the object. (The pointer has the same value as the address of the allocated memory for the standard new, but this needn't be the case for the array form new[].)

In case the memory allocation fails, an exception of type std::bad_alloc is thrown, no object is constructed and no memory is allocated.

In case the object constructor throws an exception, no object is (obviously) constructed, the memory is automatically released immediately, and the exception is propagated.

Otherwise a dynamically allocated object has been constructed, and the user must manually destroy the object and release the memory, typically by saying delete p;.

The actual allocation and deallocation function can be controlled in C++. If there is nothing else, a global, predefined function ::operator new() is used, but this may be replaced by the user; and if there exists a static member function T::operator new, that one will be used instead.

  • In Java it's fairly similar, only that the return value of new is something that can bind to a Java variable of type T (or a base thereof, such as Object), and you must always have an initializer (so you'd say T x = new T();). The object's lifetime is indeterminate, but guaranteed to be at least as long as any variables still refer to the object, and there is no way to (nor any need to) destroy the object manually. Java has no explicit notion of memory, and you cannot control the interna of the allocation.

Furthermore, C++ allows lots of different forms of new expressions (so-called placement forms). They all create dynamic-storage objects which must be destroyed manually, but they can be fairly arbitrary. To my knowledge Java has no such facilities.


The biggest difference is probably in use: In Java, you use new all the time for everything, and you have to, since it's the one and only way to create (class-type) objects. By contrast, in C++ you should almost never have naked news in user code. C++ has unconstrained variables, and so variables themselves can be objects, and that is how objects are usually used in C++.

What's the difference between overloading operator new and operator new[] in C++?

operator new is used for creating single object, such as:

my_class* p = new my_class;

operator new[] is used for creating object's array, such as:

my_class* p = new my_class[10];

Is that OK if I only overload operator new?

It depends on if you need special treatment for array version (operator new[]). By default operator new[] will call operator new.

Basically, if you need to overload operator new, it's better to overload operator new[] too, because if you want to implement your own memory management, it's a good idea to keep them all consistent. And don't forget the nothrow and placement version(as member functions only, the global version can't be displaced).

The same is true for operator delete and operator delete[].

How does new operator knows how much size needs to be allocated for specific class

The compiler has already seen the definition of Foo, so it knows how much memory is needed. new Foo turns into, essentially, two operations: operator new(sizeof Foo) to allocate the memory, followed by construction of a Foo object in the newly allocated space.

What is the difference between applying of `operator new(std::size_t);` and `new-expression`?

The new expression invokes the allocation function operator new. The allocation function obtains memory, and the new expression converts memory into objects (by constructing them).

So the following code:

T * p = new T(1, true, 'x');
delete p;

Is equivalent to the following sequence of operations:

void * addr = operator new(sizeof(T));   // allocation

T * p = new (addr) T(1, true, 'x'); // construction

p->~T(); // destruction

operator delete(addr); // deallocation

Please note that you always need a new expression to create objects (i.e. call constructors) -- constructors have no name and cannot be called directly. In this case, we use the default placement-new expression, which does nothing but create the object, unlike the non-placement form, which does both memory allocation and object construction.

C++ primer 5th edition: operator new and operator delete overloading

Most operators in C++ aren't bound by any explicit semantics or requirements. The only real exceptions are operator new and operator delete due to its specialized use, which is what this is referring to.

For example, lets consider operator==.

Even though it's conventional (and wise) to have this operator perform some kind of comparison and return a bool to indicate equality -- this actually isn't required by the C++ language.

In fact, it's actually entirely possible to define operator== to return something completely unrelated -- perhaps an int, a std::string, or something weirder like std::tuple. And, of course, it need not actually perform any comparison.

Effectively, the semantics of most operators in C++ are weakly required by convention, but not by the language.

This contrasts with operator new and operator delete. A new expression in C++ will always start a dynamic object's lifetime at the pointer returned by the operator new invocation. Whether this is new (p) T{...} for a placement-new expression, new T{...} with the global-new operator, or some new(args,...) T{...} for a custom operator new -- it must return some form of pointer to start a lifetime at for T.

Similarly, delete must end that lifetime, and call operator delete to release the underlying storage for that lifetime. This effectively forces semantics of operator new and operator delete to perform some form of allocation mechanism, and some form of cleanup mechanism respectively. It's not really possible to define new and delete to do something strange (as in the case of operator==), since the implied behavior of calling these operators will simply break (if it compiles at all).

This is why the quote mentions that the behavior of operator new/operator delete cannot be redefined; the basic meaning will always be fixed.



Related Topics



Leave a reply



Submit