重载new和delete

1. 测试代码:

 #include<iostream>
#include<new>
using namespace std;
class A {
public:
A() { cout << "A constructor" << endl; } void* operator new(size_t size)
{
cout << "this is A's new" << endl;
return ::operator new(size);
} void operator delete(void* ptr)
{
cout << "this is A's delete" << endl;
return ::operator delete(ptr);
} ~A() { cout << "A destructor" << endl; }
}; int main()
{
A *a = new A;
delete a;
return ;
}

运行结果:

虽然我们不能改变new/delete的行为,但是通过重载operator new() 和 operator delete()我们可以实现自己想要的内存管理方式,这在内存池的实现中十分关键。关于operator new有几点要注意:

(1)当无法满足所要求分配的空间时,则如果有new_handler,则调用new_handler,否则如果没要求不抛出异常(以nothrow参数表达),则执行bad_alloc异常,否则返回0

(2)重载时,返回类型必须声明为void*

(3)重载时,第一个参数类型必须为表达要求分配空间的大小(字节),类型为size_t

(4)重载时,可以带除(3)以外的其它参数

2.测试代码:

 #include <iostream>
#include <new>
#include <cstdlib>
using namespace std; void* operator new(size_t size)
{
cout << "global Override operator new: " << size << endl;
void * ptr = malloc(size);
return ptr;
} void* operator new(size_t size, int flag)
{
cout << "global Override operator new: " << size << " " << flag << endl;
return (::operator new(size));
} void operator delete (void* ptr)
{
cout << "global Override operator delete" << endl;
free(ptr);
ptr = nullptr;
} void operator delete (void* ptr, int flag)
{
cout << "Override operator delete: " << flag << endl;
::operator delete(ptr);
ptr = nullptr;
}
int main() {
int * ptr = new int();
delete ptr;
cout << endl << "*********************" << endl << endl;
ptr = new() int();
delete ptr;
return ;
}

输出结果:

定位new表达式

1. 测试代码:

 #include <iostream>
using namespace std;
char addr1[];
int main()
{
cout << "******定位new表达式演示***by David***" << endl;
char addr2[];
char *addr3 = new char[];
cout << "addr1 = " << (void*)addr1 << endl;
cout << "addr2 = " << (void*)addr2 << endl;
cout << "addr3 = " << (void*)addr3 << endl;
int *p = nullptr; p = new(addr1)int; ////把内存分配到静态区
*p = ;
cout << (void*)p << " " << *p << endl; p = new(addr2)int; ////把内存分配到栈区
*p = ;
cout << (void*)p << " " << *p << endl; p = new(addr3)int; //把内存分配到堆区
*p = ;
cout << (void*)p << " " << *p << endl;
return ;
}

运行结果:

2.测试代码

 #include <iostream>
#include <string>
#include <new> using namespace std;
const int BUF = ; class JustTesting {
private:
string words;
int number;
public:
JustTesting(const string &s = "Just Testing", int n = )
{
words = s;
number = n;
cout << words << " constructed\n";
} ~JustTesting() { cout << words << " destroyed\n"; }
void Show() const { cout << words << ", " << number << endl; }
}; int main(void)
{
char *buffer = new char[BUF]; // get a block of memory
JustTesting *pc1, *pc2; pc1 = new (buffer)JustTesting; // place object in buffer
pc2 = new JustTesting("heap1", ); // place object on heap cout << "Memory block address:\n" << "buffer: "
<< (void *)buffer << " heap: " << pc2 << endl;
cout << "Memory contents: \n";
cout << pc1 << ": ";
pc1->Show();
cout << pc2 << ": ";
pc2->Show(); JustTesting *pc3, *pc4;
pc3 = new (buffer) JustTesting("bad Idea", );
pc4 = new JustTesting("Heap2", ); cout << "Memory contents: \n";
cout << pc3 << ": ";
pc3->Show();
cout << pc4 << ": ";
pc4->Show(); delete pc2; // free heap1
delete pc4; // free heap2
delete[] buffer; // free buffer
cout << "Done\n"; return ;
}

运行结果:

参考资料

最新文章

  1. centOS 虚拟机设置固定IP:图形化设置
  2. 利用PHPMailer发送邮件时报错
  3. 2016年12-09php函数
  4. 专注docker安全:Security Scanning
  5. hdu4418(概率dp + 高斯消元)
  6. Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)
  7. C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)
  8. c++与java的优缺点
  9. js判断屏幕分辨率的代码
  10. LLVM在静态分析上的增强 @ WWDC 2013
  11. 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay. (转)
  12. spring MVC之返回JSON数据(Spring3.0 MVC)
  13. iOS多线程——GCD
  14. UWP 手绘视频创作工具技术分享系列 - SVG 的解析和绘制
  15. python粗谈面向对象(一)
  16. 2018项目UML设计-课堂实战
  17. __细看InnoDB数据落盘 图解 MYSQL
  18. PHP文件包含漏洞攻防实战
  19. for /f命令之—Delims和Tokens用法&amp;总结
  20. java文件上传-使用apache-fileupload组件

热门文章

  1. QT_地图导航 源码下载
  2. android allowbackup
  3. u-boot移植(三)---修改前工作:代码流程分析2
  4. ICS Hack Tools
  5. 下拉选择框QCombox
  6. dubbo集群服务下一台服务挂了对服务调用的影响
  7. luogu P3304 [SDOI2013]直径
  8. HDU4738 Caocao&#39;s Bridges【强连通】
  9. 7、完整版的strcpy函数
  10. vue2.0环境安装