About overloading of the C + + new operator
Do you know the difference between the new operator of C + + and operator new? Maybe you will ask, do they make a difference?
When you write the following code,
String *pa = new String ("Memory Managerment");
You are using the new operator, which, like sizeof, is supported by the C + + language level. You can't change the semantics of it, it does things always the same: allocate enough memory to hold the object, and then call the constructor to initialize the memory allocated in the previous step. The NEW operator always does both things, and you can't change its behavior.
All you can change is the first step, how to allocate raw memory for an object. The operator new function is used to allocate raw memory for an object. The first step of the new operator is to call operator new. You can overload this function. Its prototype is as follows:
void* operator new (size_t size);
The return value of the function is void*, because the function returns a pointer. This pointer points to native, initialized memory. Its semantics are like malloc. In fact, it is called malloc inside. The parameter size specifies the amount of memory to be allocated. You can add additional parameters to the overload, but the first parameter type must be size_t.
In most cases, you don't need to call operator new, in case you need to call it, the format of the call is this:
void* rawmemory = operator new (sizeof (string));
function operator new returns a pointer to a piece of memory that is sufficient to hold a string object.
Just like malloc, the only responsibility for operator new is to allocate memory, which knows nothing about constructors. Constructing an uninitialized pointer returned by operator new to an object is the work of the new operator. When your compiler encounters the following code:
String *pa = new String ("Memory Managerment");
The pseudo code class it generates is as follows:
void* memory = operator new (sizeof (string));
Call String::string ("Memory Managerment") on memory;
string* Pa = static_cast<string*> (memory);
The second cloth contains the call to the constructor. This is called by your compiler. So you're not going to ask, can a programmer call a constructor manually? The answer is in the negative. But the compiler provides you with another compromise that you can achieve.
It is meaningless to call a constructor on an existing object. Because the constructor is used to initialize the object. But sometimes some of the memory has been allocated but not initialized, and you need to construct an object in those memory. You can use a special version of the operator new function. A term called placement new functions to do this thing.
Back to the example of the previous string, we can use placement NEW:
void* memory = operator new (sizeof (string));
string* PA = new (Memory) string ("Memory Managerment");
The above two sentences are equivalent to what the new operator does.
This is the whole secret of operator new and placement new. Generally speaking, you do not need to overload and explicitly call these two functions.