New, operator new, and placement new in C + +, new
- New (also known as new operator), which is the new operator and cannot be overloaded
classnew//此时的new ,是new 操作符
The new operation performs the following three steps
- Call Class (if overloaded) or global operator new allocate space
- call the constructor with the parameter list of the column following the type to generate the class object
- Returns the corresponding pointer
Second, operator new
operator new is a operator function, similar to operator + functions, which can be overloaded , and operator new is typically overloaded in a class. In the global overload it is easy to cause the program to crash because the global:: operator new is responsible for allocating heap space during the entire program run, overloading the global:: operator new must be cautious!
Operator new overloads in the class, completes the custom operation, and calls Global:: operator new, returns the corresponding pointer
class T{ ... void* operator new(size_t){ ... //自定义操作 return ::operator new(size_t); }};
Third, placement new
is a standard, global version of the overloaded operator new
It can not be replaced by a custom version, that is, it cannot be overloaded . Its purpose is to construct objects in the allocated space.
void *operatornewvoidthrowreturn p; }
Placement New Use steps
In many cases, the use of placement new differs from other common new. This provides the steps to use it.
char*buf = (::operatornew((size_t)(sizeof(T))));
- Object, the constructor is called in the allocated cache, and the object is generated
new(buf)T;
- Accessing the members of an object, using objects
t->men_func();
- Call an external destructor
t->~T();
delete [] buf;
Examples of Use:
#include <stdio.h>#include <iostream>#include <string>#include <malloc.h>using namespaceStdclasstestnew{ Public: Testnew () {cout <<"Testnew::testnew () constructor was executed"<< Endl; } ~testnew () {cout <<"Testnew::~testnew () destructor was executed"<< Endl; }void*operator New(size_t size, string str) {cout <<"Overloaded 1:testnew::op new,"<< str << Endl;return::operator New(size); }void*operator New(size_t size) {cout <<"overloaded 2:testne w::op new,without str"<< Endl;return::operator New(size); }voidPrint () {cout <<"initialized successfully"<< Endl; }};void*operator New(size_t size) {cout <<":: Op new memory allocation"<< Endl;returnmalloc (size);}intMain () {cout <<"Overloaded Global:: Op new"<< Endl;Char* BUF = (Char*)(::operator New((size_t) (sizeof(testnew))); cout << Endl; cout <<"Placement new"<< Endl;//Not added::, will call void* Testnew:: operator new (size_t size, string str) //causes the placement new to not match the globalTestnew *test =::New(BUF) testnew; Test->print (); Test->~testnew ();Delete[]buf; cout << Endl; cout <<"Overloaded Testnew::op new 1"<< Endl;//Output has 4 rows at this timeTestnew *test2 =New("with str") Testnew;//::op new memory allocation, assigning heap space to const char* "overloaded" //Overload 1:testnew::op new,with STR---Call Testnew::op new 1 //::op New Memory allocation->TESTNEW::OP new 1 calls Global:: Op new //executed the Testnew::testnew () constructorTest2->print ();//Output "initialized successfully", indicating that the pointer was returned correctlycout << Endl; cout <<"Overloaded Testnew::op new 2"<< Endl; Testnew *test3 =NewTestnew; Test3->print ();//Output "initialized successfully", indicating that the pointer was returned correctlycout << Endl; cout <<"Destruction"<< Endl;DeleteTest2;DeleteTest3; GetChar ();return 0;}
?
- Original all, reproduced annotated source, if there are errors, welcome correction, Common learning. Thank you!
Summary: New, operator new, and placement new in C + +