写了placement new就要写placement delete
“placement new”通常是专指指定了位置的new(std::size_t size, void *mem)
,用于vector
申请capacity
剩余的可用内存。 但广义的”placement new”指的是拥有额外参数的operator new
。
new
和delete
是要成对的,因为当构造函数抛出异常时用户无法得到对象指针,因而delete
的责任在于C++运行时。 运行时需要找到匹配的delete
并进行调用。因此当我们编写了”placement new”时,也应当编写对应的”placement delete”, 否则会引起内存泄露。在编写自定义new
和delete
时,还要避免不小心隐藏它们的正常版本。
成对的delete
当构造函数抛出异常时,C++会调用与new
同样签名的delete
来撤销new
。但如果我们没有声明对应的delete
:
class Widget{
public:
static void* operator new(std::size_t size, std::ostream& log) throw(std::bad_alloc);
Widget(){ throw 1; }
};
Widget *p = new(std::cerr) Widget;
构造函数抛出了异常,C++运行时尝试调用delete(void *mem, std::ostream& log)
, 但Widget
没有提供这样的delete
,于是C++不会调用任何delete
,这将导致内存泄露。 所以在Widget
中需要声明同样签名的delete
:
static void operator delete(void *mem, std::ostream& log);
但客户还可能直接调用delete p
,这时C++运行时不会把它解释为”placement delete”,这样的调用会使得Widget
抛出异常。 所以在Widget
中不仅要声明”placement delete”,还要声明一个正常的delete
。
class Widget{
public:
static void* operator new(std::size_t size, std::ostream& log) throw(std::bad_alloc);
static void operator delete(void *mem, std::ostream& log);
static void operator delete(void *mem) throw();
Widget(){ throw 1; }
};
这样,无论是构造函数抛出异常,还是用户直接调用delete p
,内存都能正确地回收了。
名称隐藏
在Item 33中提到,类中的名称会隐藏外部的名称,子类的名称会隐藏父类的名称。 所以当你声明一个”placement new”时:
class Base{
public:
static void* operator new(std::size_t size, std::ostream& log) throw(std::bad_alloc);
};
Base *p = new Base; // Error!
Base *p = new (std::cerr) Base; // OK
普通的new
将会抛出异常,因为”placement new”隐藏了外部的”normal new”。同样地,当你继承时:
class Derived: public Base{
public:
static void* operator new(std::size_t size) throw(std::bad_alloc);
};
Derived *p = new (std::clog) Derived; // Error!
Derived *p = new Derived; // OK
这是因为子类中的”normal new”隐藏了父类中的”placement new”,虽然它们的函数签名不同。 但Item 33中提到,按照C++的名称隐藏规则会隐藏所有同名(name)的东西,和签名无关。
最佳实践
为了避免全局的”new”被隐藏,先来了解一下C++提供的三种全局”new”:
void* operator new(std::size_t) throw(std::bad_alloc); // normal new
void* operator new(std::size_t, void*) throw(); // placement new
void* operator new(std::size_t, const std::nothrow_t&) throw(); // 见 Item 49
为了避免隐藏这些全局”new”,你在创建自定义的”new”时,也分别声明这些签名的”new”并调用全局的版本。 为了方便,我们可以为这些全局版本的调用声明一个父类StandardNewDeleteForms
:
class StandardNewDeleteForms {
public:
// normal new/delete
static void* operator new(std::size_t size) throw(std::bad_alloc) { return ::operator new(size); }
static void operator delete(void *pMemory) throw() { ::operator delete(pMemory); }
// placement new/delete
static void* operator new(std::size_t size, void *ptr) throw() { return ::operator new(size, ptr); }
static void operator delete(void *pMemory, void *ptr) throw() { return ::operator delete(pMemory, ptr); }
// nothrow new/delete
static void* operator new(std::size_t size, const std::nothrow_t& nt) throw() { return ::operator new(size, nt); }
static void operator delete(void *pMemory, const std::nothrow_t&) throw() { ::operator delete(pMemory); }
};
然后在用户类型Widget
中using StandardNewDeleteForms::new/delete
即可使得这些函数都可见:
class Widget: public StandardNewDeleteForms { // inherit std forms
public:
using StandardNewDeleteForms::operator new;
using StandardNewDeleteForms::operator delete;
static void* operator new(std::size_t size, std::ostream& log) throw(std::bad_alloc); // 自定义 placement new
static void operator delete(void *pMemory, std::ostream& logStream) throw(); // 对应的 placement delete
};
写了placement new就要写placement delete的更多相关文章
- 条款十: 如果写了operator new就要同时写operator delete
为什么有必要写自己的operator new和operator delete? 答案通常是:为了效率.缺省的operator new和operator delete具有非常好的通用性,它的这种灵活性也 ...
- Hibernate写hql语句与不写hql语句的区别?
写hql语句与不写hql语句的区别? 写hql语句:书写HQL语句,所有的查询与投影的设计均使用HQL语句完成. 不写hql语句:没有任何查询语句,所有的查询与投影的设计使用面向对象格式完成. 二者选 ...
- [改善Java代码]覆写equals方法必须覆写hashCode方法
覆写equals方法必须覆写hashCode方法,这条规则基本上每个Javaer都知道,这也是JDK API上反复说明的,不过为什么要这样做呢?这两个方法之间有什么关系呢?本建议就来解释该问题,我们先 ...
- 写文件前, 检查目录写权限(PHP)
写文件前, 检查目录写权限 写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你很多调试时间. linux系统中, 需要处理权限, 目录权限不当会导致很多很多的问题, 文件也 ...
- 为什么覆写equals必须要覆写hashCode?
============================================= 原文链接: 为什么覆写equals必须要覆写hashCode? 转载请注明出处! ============= ...
- Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 v4
Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 1. 原则 2 1.1. 有深度, 有广度 2 1.2. 业务通用性有通用性 尽可能向上抽象一俩层..业务通用性与语言通用性. 2 ...
- Atitit.如何文章写好 论文 文章 如何写好论文 技术博客
Atitit.如何文章写好 论文 文章 如何写好论文 技术博客 1. 原则 1 1.1. 有深度, 有广度 1 1.2. 业务通用性有通用性 尽可能向上抽象一俩层..业务通用性与语言通用性. 2 ...
- python手写神经网络实现识别手写数字
写在开头:这个实验和matlab手写神经网络实现识别手写数字一样. 实验说明 一直想自己写一个神经网络来实现手写数字的识别,而不是套用别人的框架.恰巧前几天,有幸从同学那拿到5000张已经贴好标签的手 ...
- 用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1
package com.ljn.base; /** * @author lijinnan * @date:2013-9-12 上午9:55:32 */ public class IncDecThrea ...
随机推荐
- ZooKeeper个人笔记之节点的监听
create public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) th ...
- Linux CentOS6.5下安装Oracle ASM
Oracle版本:Oracle 11g 1.确定自己的Linux版本: [root@localhost ~]#uname -r 2.6.32-431.el6.x86_64 2.6.32-431.el6 ...
- Linux文件系统扩容步骤
1 扩容前检查 cat /etc/fstab df -h 在扩容之前请确认VG的Free大小,以及文件和文件系统是否达到系统限制 2 系统识别硬盘 #echo "- - -" &g ...
- Ubuntu不显示壁纸,桌面右键无反应解决
用ubuntu tweak调整ubuntu的桌面图标显示,导致桌面无法显示壁纸,桌面点击右键无发应。 解决办法:Ubuntu Tweak中“调整”选项卡-》”显示桌面图标“的选项一定要打开,处于ON状 ...
- mysql timeout connection
由于使用阿里云服务器,使用mysql 每当周一的时候客户端首次连,总是报timeout connection 的错误 ,尝试了几个方法没有实际效果. 1.用网上说的URl上缀上autoReconnec ...
- ajax pagination 布局刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Android 上传图片并添加参数 PHP接收
php端接收代码: public function get_file(){ $local_path = "./Public/daixu_picture/figure/";//服务器 ...
- Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析
mongodb和memcached不是一个范畴内的东西.mongodb是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据.mongodb和memcached不存在谁替换谁的问题. 和 ...
- Bootstrap的字体文件woff2 报错
在iis上看到网站有个404,于是强迫症来了... 百度了许久,知道了解决办法,记下来,是网站默认没有支持这种字体 在 Web.config 的 system.webServer 节点中添加: 带标签 ...
- PostgreSQL windows service启动失败
from: http://stackoverflow.com/questions/1251233/unable-to-run-postgresql-as-windows-servicepg_ctl - ...