new和delete重载

时间:2024-06-14 16:36:32

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 = ;
//用于存储Test对象的静态内存区,注意为static变量
static char c_buffer[];
//用于标识内存空间的使用情况
static char c_map[]; public: //重载new操作符,将对象分配在静态区(而非堆上!)
void* operator new (unsigned int size) //默认为静态函数
{
void* ret = NULL;
//查找静态区中的内存空闲
for(int i=; i<COUNT; i++)
{
if(!c_map[i]) //空闲时
{
c_map[i] = ; //标志为正在使用 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 == ) && ( <= index) && (index < COUNT) )
{
c_map[index] = ; //标记为空闲
} cout <<"succeed to free memory: " << p << endl;
} //函数返回后,编译器自动生成调用析构函数的代码
}
}; char Test::c_buffer[sizeof(Test) * Test::COUNT] = {};
char Test::c_map[Test::COUNT] = {}; 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[] = {}; //模拟内存管理,因内存区最多只能分配4个Test对象,当申请第5个对象
//时,会返回NULL。这个例子也可结合二阶构造来生成《多例模式》
for( int i=; i<; i++)
{
pa[i] = new Test;
cout << "pa[" <<i <<"] = " << pa[i] << endl;
} //释放
for( int i=; i<; i++)
{
cout << "delete " << pa[i] << endl;
delete pa[i];
} return ;
}
/*输出结果:

===== 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

*/

new的两个主要任务:1.分配内存,2.调用构造函数初始化。而上例子中的operator new函数却只见分配内存,不见初始化工作,这看起来有点不可思议。其实重载的operator new函数确实只需完成前一半的功能,那初始化工作在什么时候实现的呢?答案是当调用new Test时,会先调用operator new,之后编译器自动地为我们插入了初始化的代码。 这也是new操作符与一般的操作符重载的不同之处·