Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "::operator new" and "::operator delete"?

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?

like image 258
Amir Saniyan Avatar asked May 09 '12 09:05

Amir Saniyan


People also ask

What is :: operator new?

The new operator invokes the function operator new . For arrays of any type, and for objects that aren't class , struct , or union types, a global function, ::operator new , is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis.

What is new operator and delete operator?

These operators allocate memory for objects from a pool called the free store (also known as the heap). The new operator calls the special function operator new , and the delete operator calls the special function operator delete .

What is operator delete in C++?

Using the delete operator on an object deallocates its memory. A program that dereferences a pointer after the object is deleted can have unpredictable results or crash.

What is new operator in C++?

The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.


1 Answers

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*).

like image 130
Emilio Garavaglia Avatar answered Oct 07 '22 13:10

Emilio Garavaglia