new和delete重载

1. 简介

  • new/delete关键字,其本质是预定义的操作符,因此支持重载
  • 默认new和delete的行为:
    • new:    ①获取内存空间(默认为堆空间);②在获取的空间中调用构造函数创建对象
    • delete: ①调用析构函数销毁对象;②归还对象所占用的空间(默认为堆空间)

2. C++中new/delete操作符重载的两种方式

  • 全局重载:会影响所有的类(不推荐
  • 局部重载:针对具体类进行重载
  • new/delete重载的:改变的只是内存的分配方式(即new出来的对象分配不一定在堆中)


new和delete的重载函数:

//new/delete会被默认的定义为静态成员函数,哪怕在定义时没有显式指出。因为在调用new的时候,对象还
//没创建出来,所以只能通过静态成员函数来调用。
 
//静态成员函数(即使没写static,也会被自动声明为static函数)
void* operator new (unsigned int size)//有的编译器 size_t size
{
    void* ret = NULL;
     
    /*ret指向一片刚分配好的内存*/
     
    return ret;
}
 
//静态成员函数
void operator delete(void* p)
{
    /*释放p所指定的内存空间*/
}

【编程实验】静态存储区中创建动态对象

#include <iostream>

using namespace std;

class Test
{
    //存储在静态区中Test对象的最大数量
    static const unsigned int COUNT = 4;
    //用于存储Test对象的静态内存区,注意为static变量
    static char c_buffer[];
    //用于标识内存空间的使用情况
    static char c_map[];
    
public:

    //重载new操作符,将对象分配在静态区(而非堆上!)
    void* operator new (unsigned int size) //默认为静态函数
    {
        void* ret = NULL;
        //查找静态区中的内存空闲
        for(int i=0; i<COUNT; i++)
        {
            if(!c_map[i]) //空闲时
            {
                c_map[i] = 1; //标志为正在使用
                
                ret = c_buffer + i * sizeof(Test);
                cout << "success to allocate memory: " << ret << endl;
               
                break;
            }
        }
        
        return ret; //当返回后,编译器继继会生成调用构造函数来初始化
                    //这片内存空间的代码。
    }
    
    //重载delete操作符,将释放对应的内存空间(标志为空闲)
    void operator delete (void* p)
    {
        if( p != NULL)
        {
            char* mem = reinterpret_cast<char*>(p);
            int index = (mem - c_buffer) / sizeof(Test);
            int flag = (mem - c_buffer) % sizeof(Test);//传入的是对象地址?
            
            if( (flag == 0) && (0 <= index) && (index < COUNT) )
            {
                c_map[index] = 0; //标记为空闲
            }
            
            cout <<"succeed to free memory: " << p << endl;
        }
        
        //函数返回后,编译器自动生成调用析构函数的代码
    }
};

char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0};
char Test::c_map[Test::COUNT] = {0};

int main()
{
    cout << "===== Test Single Object =====" << endl;
    
    Test* pt = new Test; //相当于(Test*)Test::operator new(sizeof(Test));
    delete pt;
    
    cout << "===== Test Object Array =====" << endl;
    Test* pa[5] = {0};
    
    //模拟内存管理,因内存区最多只能分配4个Test对象,当申请第5个对象
    //时,会返回NULL。这个例子也可结合二阶构造来生成《多例模式》
    for( int i=0; i<5; i++)
    {
        pa[i] = new Test;
        cout << "pa[" <<i <<"] = " << pa[i] << endl;
    }
    
    //释放
    for( int i=0; i<5; i++)
    {
        cout << "delete " << pa[i] << endl;
        delete pa[i];
    }
    
    return 0;
}
/*输出结果:
===== Test Single Object =====
success to allocate memory: 00007FF60CA54600
succeed to free memory: 00007FF60CA54600
===== Test Object Array =====
success to allocate memory: 00007FF60CA54600
pa[0] = 00007FF60CA54600
success to allocate memory: 00007FF60CA54601
pa[1] = 00007FF60CA54601
success to allocate memory: 00007FF60CA54602
pa[2] = 00007FF60CA54602
success to allocate memory: 00007FF60CA54603
pa[3] = 00007FF60CA54603
pa[4] = 0000000000000000
delete 00007FF60CA54600
succeed to free memory: 00007FF60CA54600
delete 00007FF60CA54601
succeed to free memory: 00007FF60CA54601
delete 00007FF60CA54602
succeed to free memory: 00007FF60CA54602
delete 00007FF60CA54603
succeed to free memory: 00007FF60CA54603
delete 0000000000000000
*/

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值