C++ new和delete重载

参考文章:http://blog.163.com/hbu_lijian/blog/static/126129153201211510361484/

    http://blog.csdn.net/hzyong_c/article/details/5949314

首先,new和delete是运算符,重载new和delete是可能的。这样做的原因是,有时希望使用某种特殊的动态内存分配方法。例如,可能有些分配子程序,他们的堆已耗尽,自动开始把一个磁盘文件当虚存储使用,或者用户希望控制某一片存储空间的分配等。

先看一下重载new/delete的规则:

重载的operator new的参数个数任意,但第一个参数必须是size_t类型的,返回值必须是void*。重载operator delete只允许有一个参数,且是void*型。

void *operator new (size_t size)
{
  .......//完成分配工作
  return pointer_to_memory;
}

void operator delete(void *p)
{
  ......//释放由p指向的存储空间
}

1.局部重载new和delete(可以使用成员函数和友元函数两种方式重载)
使用new分配某个重载了new的累的对象空间时,先调用new的重载函数,再调用该类的构造函数,如果该类的构造函数有参数要求,则必须给出对应的实参。
使用了delete释放某个重载了delete的累的对象空间时,先调用类的析构函数,然后再调用重载的delete函数。
#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.全局重载new和delete
可以在任何类说明之外重在new和delete,使它们成为全局的。当new和delete被重载为全局时,C++原来的new与delete被忽略,并且重载的运算符用于所有类型(包括标准型和用户定义类型)的分配要求。
#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;
}

实际应用中可能需要在new中作更多的处理,内存检测等,new重载时也可以加入其他的参数,例如__FILE__、__LINE__等,以便于快速定位问题。

你可能感兴趣的:(C++ new和delete重载)