What are "::operator new" and "::operator delete"?
Asked Answered
P

4

15

I know new and delete are keywords.

int obj = new int;
delete obj;

int* arr = new int[1024];
delete[] arr;

<new> header is a part of C++ standard headers. It has two operators (I am not sure they are operators or they are functions):

::operator new

::operator delete

these operators used like below:

#include <new>
using namespace std;

int* buff = (int*)::operator new(1024 * sizeof(int));
::operator delete(buff);

What are "::operator new" and "::operator delete"? Are they different from new and delete keywords?

Phares answered 9/5, 2012 at 9:35 Comment(0)
B
13

:: tells the compiler to call the operators defined in global namespace.
It is the fully qualified name for the global new and delete operators.

Note that one can replace the global new and delete operators as well as overload class-specific new and delete operators. So there can be two versions of new and delete operators in an program. The fully qualified name with the scope resolution operator tells the compiler you are referring to the global version of the operators and not the class-specific ones.

Bookbinding answered 9/5, 2012 at 9:36 Comment(5)
But programmer should include new header.Phares
@AmirSaniyan The only reason (I know of) to include the new header if is you wish to use placement newOverlord
@AmirSaniyan: Good Read: When is #include <new> library required in C++?Bookbinding
@Als As Benj said: if you're using one of the standard placement new's. Or if you need access to std::bad_alloc, or one of the few other functions declared in the header. It's not needed for the standard non-placement new, regardless of whether you write just new MyType or ::new MyType, and it's not needed if you're new expression resolves to a user defined operator new, rather than one in the standard library.Subroutine
@JamesKanze: True & so i linked the relevant Q as an comment, One of the answers there(not the accepted one) clears this aptly.Bookbinding
I
26

the new keyword (used alone) is not the same as the operator new function.

Calling

Object* p = new Object(value);

is equvalent in calling

void* v = operator new(sizeof(Object));
p = reinterpret_cast<Object*>(v);
p->Object::Object(value); //this is not legal C++, it just represent the implementation effect

The operator new (or better the void* operator new(size_t) variant) just allocate memory, but does not do any object construction.

The new keyword calls the operator new function, but then calls the object constructor.

To separate allocation from contruction, a variant of operator new is declared as

void* operator new(size_t, void* at)
{ return at; }

and the previous code is typically written as

Object* p = reinterpret_cast<Object*>(operator new(sizeof(Object))); //no contruction here
new(p) Object(value); //calls operator new(size_t, void*) via keyword

The operator new(size_t, void*) does nothing in itself, but, being invoked by the keyword will result in the contructor being called.

Reversely, destruction and deallocation can be separated with

p->~Object();
operator delete(p); //no destructor called

instead of delete p; that calls the destructor and then operator delete(void*).

Isothere answered 9/5, 2012 at 10:31 Comment(0)
B
13

:: tells the compiler to call the operators defined in global namespace.
It is the fully qualified name for the global new and delete operators.

Note that one can replace the global new and delete operators as well as overload class-specific new and delete operators. So there can be two versions of new and delete operators in an program. The fully qualified name with the scope resolution operator tells the compiler you are referring to the global version of the operators and not the class-specific ones.

Bookbinding answered 9/5, 2012 at 9:36 Comment(5)
But programmer should include new header.Phares
@AmirSaniyan The only reason (I know of) to include the new header if is you wish to use placement newOverlord
@AmirSaniyan: Good Read: When is #include <new> library required in C++?Bookbinding
@Als As Benj said: if you're using one of the standard placement new's. Or if you need access to std::bad_alloc, or one of the few other functions declared in the header. It's not needed for the standard non-placement new, regardless of whether you write just new MyType or ::new MyType, and it's not needed if you're new expression resolves to a user defined operator new, rather than one in the standard library.Subroutine
@JamesKanze: True & so i linked the relevant Q as an comment, One of the answers there(not the accepted one) clears this aptly.Bookbinding
S
7

They are allocator and deallocator functions. The new operator does two things: it calls an allocator function to get the memory, and it calls the constructor of the object. The delete operator also does two things: it calls the destructor, and then calls the a deallocator function. The default allocator function is ::operator new, and the default deallocator function is ::operator delete. Both can be replaced by the user.

Note that in a new expression, the ::operator new function is looked up in more or less the same manner as it would be if it were a normal function called from within a member function. As for normal functions, you can qualify the operator to change the lookup: new MyClass will find a member operator new if one is present; ::new MyClass will use the default allocator, even if MyClass defines a member operator new.

Subroutine answered 9/5, 2012 at 9:57 Comment(1)
+1 for distinguishing between the allocation function and the new-expression. Both use the new keyword in their syntax.Justis
I
3

:: means just a global namespace

Infix answered 9/5, 2012 at 9:37 Comment(2)
when new is a keyword, why should I use global namespace?Phares
You shouldn't it's show that you are using new from global namespace. But you can override your new version then you will use your namespace if you needInfix

© 2022 - 2024 — McMap. All rights reserved.