C + + overloaded New,delete operator placement NEW

Source: Internet
Author: User

Overloaded New,delete operators New,delete are also classified as operators in C + +, so they can be overloaded. Behavior of NEW:
    • Open up memory space first

    • Call the constructor of the class again

The part that opens up the memory space can be overloaded. The behavior of Delete:
    • Call the class's destructor first

    • and free up memory space

Frees the portion of memory space that can be overloaded. Why would you want to reload them? Sometimes the memory pools need to be implemented by overloading them. Frequent new and delete objects, will cause memory fragmentation, memory shortage and other issues, affecting the normal execution of the program, so one time to open up a suitable large space, each time you need the object, no longer need to open up memory space, only need to call the constructor (using placement new). The overloaded function of New,delete can be a global function or a public overloaded function inside a class, and when there are global overloaded functions and public overloaded functions inside the class, The actual call is the public overloaded function inside the class. New,delete can be overloaded in many ways, but the first parameter of the new function must be a size_t type

Overloaded Mode 1,new Single object

void* operator new(size_t sz){  void* o = malloc(sz);  return o;}void operator delete(void *o){  free(o);}

Overloaded mode 2,new an array of objects

void* operator new[](size_t sz){  void* o = malloc(sz);  return o;}void operator delete[](void *o){  free(o);}
Overloaded mode 3, does not open up space, just call the given object (with address recognition) of the construction method, also called Placement New
//第一个参数size_t即使不使用,也必须有                          void* operator new(size_t sz, String* s, int pos){  return s + pos;}

Small example:

#include <iostream> #include <string.h>using namespace Std;class string{public:string (const char* str = "") {    cout << "Create" << Endl;      if (NULL = = str) {data = new char[1];    Data[0] = ' + ';      } else{data = new Char[strlen (str) + 1];    strcpy (data, str);    }} ~string () {cout << "free" << Endl;    delete []data;  data = NULL; }private:char* data = null;};/  /heavy-duty mode 1void* operator new (size_t sz) {cout << "in operator new" << Endl;  void* o = malloc (SZ); return o;}  void operator delete (void *o) {cout << "in operator delete" << Endl; Free (o);}  Overloaded mode 2void* operator new[] (size_t sz) {cout << "in operator new[]" << Endl;  void* o = malloc (SZ); return o;}  void operator delete[] (void *o) {cout << "in operator delete[]" << Endl; Free (o);} Overloaded mode 3//first parameter size_t even if not applicable, there must be void* operator new (size_t sz, string* s, int pos) {return S + pos ;} int main () {String *s = new String ("abc");  Delete S;  String *SR = new String[3];  delete []SR;  Open the memory pool, but have not yet called the construction method of the object in the pool String *ar = (string*) operator new (sizeof (String) * 2);  Call dominant the construction method of the first object, no longer opens space new (AR, 0) String ("First0");  Call dominant the constructor of the second object, no longer opens up a space new (AR, 1) String ("First1");  Call dominant the first object's destructor, and note that it will not be released to memory (&ar[0])->~string ();  Call dominant the second object's destructor, and note that it will not be released to memory (&ar[1])->~string (); The objects in the memory pool can be reused operator delete (AR) before the following statement is executed;}

C + + overloaded New,delete operator placement new

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.