C + + New/delete overload __c++

Source: Internet
Author: User
First, new and delete are operators, and overloading new and delete is possible. The reason for this is that you sometimes want to use a particular dynamic memory allocation method. For example, there may be some allocation subroutines, their heap is depleted, automatically start to use a disk file as virtual storage, or users want to control the allocation of a piece of storage space.

The format for overloading new and delete is as follows:

void *operator New (size_t size)
{
  ...//Completing the assignment return
  pointer_to_memory;
}
void operator delete (void *p)
{
  ...//release storage space that is pointed by P
}
1. Local overload new and delete (you can overload it in two ways using member functions and friend functions)
When you use new to assign a tired object space that is overloaded with new, call the overloaded function of new, and then call the constructor of the class, and the corresponding argument must be given if the class's constructor has parameter requirements.
When you use Delete to release a tired object space that is overloaded with delete, you first call the class's destructor, and then call the overloaded delete function.
#include <iostream> #include <stdlib.h> #include <string.h> using namespace std;
    Class Three_d {private:int x,y,z; public:three_d (int a,int b,int c);
    ~three_d () {cout << "destructing\n";
    } void *operator New (size_t size);
    void operator delete (void *p);
Friend Ostream & operator << (ostream &stream,three_d obj);
};
    three_d::three_d (int a,int b,int c) {cout << "constructing\n";
    x = A;
    y = b;
z = c;
    } void *three_d::operator New (size_t size) {cout << "in Threee_d new\n";
return malloc (size);
    } void Three_d::operator Delete (void *p) {cout << "in Three_d delete\n";
Free (p);
    } ostream &operator << (ostream &os,three_d obj) {os << obj.x << ",";
    OS << obj.y << ",";
    OS << obj.z << "\ n";
return OS;
    int main (int argc,char *argv[]) {three_d *p = new Three_d (1,2,3); Three_d *p1 = new Three_D (4,5,6);
        if (!p | |!p1) {cout << "allocation failure" << Endl;
    return 1;
    } cout << *p << *p1;
    Delete p;
    Delete P1;
    int *pnum;
    Pnum = new int;
    *pnum = 0;
    cout << "num =" << *pnum << Endl;
    Delete Pnum;
    cout << "Application Run successfully!" << Endl;
return 0; }
2. Global overload new and delete
You can make them global by resetting the new and delete in addition to any class description. When new and delete are overloaded as global, the original new and delete of C + + are ignored, and the overloaded operators are used for allocation requirements for all types, including standard and user-defined types.
#include <iostream> #include <stdlib.h> #include <string.h> using namespace std;
    Class Three_d {private:int x,y,z; public:three_d (int a,int b,int c);
    ~three_d () {cout << "destructing\n";
Friend Ostream & operator << (ostream &stream,three_d obj);
};
    three_d::three_d (int a,int b,int c) {cout << "constructing\n";
    x = A;
    y = b;
z = c;
    } void *operator New (size_t size) {cout << "in Threee_d new\n";
return malloc (size);
    } void operator delete (void *p) {cout << "in Three_d delete\n";
Free (p);
    } ostream &operator << (ostream &os,three_d obj) {os << obj.x << ",";
    OS << obj.y << ",";
    OS << obj.z << "\ n";
return OS;
    int main (int argc,char *argv[]) {three_d *p = new Three_d (1,2,3);
    Three_d *p1 = new Three_d (4,5,6); if (!p | |!p1) {cout << "allocation failure" << Endl;
    return 1;
    } cout << *p << *p1;
    Delete p;
    Delete P1;
    int *pnum;
    Pnum = new int;
    *pnum = 0;
    cout << "num =" << *pnum << Endl;
    Delete Pnum;
    cout << "Application Run successfully!" << Endl;
return 0; }

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.