Summary: New, operator new, and placement new in C + +

Source: Internet
Author: User

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

  1. Call Class (if overloaded) or global operator new allocate space
  2. call the constructor with the parameter list of the column following the type to generate the class object
  3. 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.

  • Cache Advance Allocation
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();
  • Freeing resources
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 + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.