📂c++
🔖c++
2023-02-13 10:44阅读: 273评论: 0推荐: 0

重载new&delete运算符

重载new&delete运算符

重载new和delete运算符的目是为了自定义内存分配的细节。(内存池:快速分配和归还,无碎片)

在C++中,使用new时,编译器做了两件事情:

1)调用标准库函数operator new()分配内存;

2)调用构造函数初始化内存;

使用delete时,也做了两件事情:

1)调用析构函数;

2)调用标准库函数operator delete()释放内存。

构造函数和析构函数由编译器调用,我们无法控制。

但是,可以重载内存分配函数operator new()和释放函数operator delete()。

1)重载内存分配函数的语法:void* operator new(size_t size);

参数必须是size_t,返回值必须是void*。

2)重载内存释放函数的语法:void operator delete(void* ptr)

参数必须是void *(指向由operator new()分配的内存),返回值必须是void。

重载的new和delete可以是全局函数,也可以是类的成员函数。

为一个类重载new和delete时,尽管不必显式地使用static,但实际上仍在创建static成员函数。

编译器看到使用new创建自定义的类的对象时,它选择成员版本的operator new()而不是全局版本的new()。

void* operator new(size_t size)   // 参数必须是size_t(unsigned long long),返回值必须是void*。
{
	cout << "调用了全局重载的new:" << size << "字节。\n";
	void* ptr = malloc(size);        // 申请内存。
	cout << "申请到的内存的地址是:" << ptr << endl;
	return ptr;
}
                
void operator delete(void* ptr)   // 参数必须是void *,返回值必须是void。
{
	cout << "调用了全局重载的delete。\n";
	if (ptr == 0) return;       // 对空指针delete是安全的。
	free(ptr);      // 释放内存。
}

本文作者:jinganglang567

本文链接:https://www.cnblogs.com/tgfoven/p/17115573.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   jinganglang567  阅读(273)  评论(0编辑  收藏  举报
  1. 1 look after you louis tomlinson
  2. 2 just hold on louis tomlinson
  3. 3 i can steven cooper
  4. 4 rock me one direction
  5. 5 just can't let her go one direction
  6. 6 this town Niall Horan
  7. 7 cut in love july
  8. 8 nemo 夜愿
  9. 9 in the end Black Veil Brides
  10. 10 glad you came the wanted
  11. 11 chasing the sun the wanted
  12. 12 TAKE MY HAND simple plan
  13. 13 wish i had an angel 夜愿
just hold on - louis tomlinson
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Sasha Alex Sloan/Eric Rosse

作曲 : Steve Aoki/Louis Tomlinson/Sasha Alex Sloan/Nolan Lambroza/Eric Rosse

Wish that you could build a time machine

So you could see

The things no one can see

Feels like you're standing on the edge

Looking at the stars

And wishing you were them

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

It's not over until it's all been said

It's not over until your dying breath

So what do you want them to say when you're gone?

That you gave up or that you kept going on?

What do you do when a chapter ends?

Do you close the book and never read it again?

Where do you go when your story's done?

You can be who you were or who you'll become

Oooh

If it all goes wrong

Oooh

Darling just hold on

The sun goes down and it comes back up

The world it turns no matter what

Oooh

If it all goes wrong

Darling just hold on

Oooh

Darling just hold on

Oooh

Oooh

If it all goes wrong

Oooh

Darling just hold on

Oooh

If it all goes wrong

Darling just hold on

点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起