自定义的new操作符是怎么对英语new 一个对象的?自定义的delete操作符什么情况下得到调用?new一个对象时出现异常需要我操心内存泄露吗?下面的一个例子帮我们解开所有的疑惑。

1. 调用规则  new(A,B) class(param)  -> operator new(sizeof(class), A, B)

2. 自定义的操作符只用new对象异常的时候才会得到调用机会,而且调用哪个delete和你用的那个new一一对应,

规则是 new(X, Y) class(param);  -> delete(X, Y) class;-> operator delete(X, Y, void* p)

3. 我们不用担心new对象时候引发的异常的内存泄露,c++系统会我们在异常发生的第一时间(哪怕我们定义了异常处理函数)帮助我们释放内存。

/*
运行结果:
Normal operator new called.
Normal operator delete called. Custom operator new called.
Normal operator new called.
Normal operator delete called. Normal operator new called.
Normal operator delete called.
exception Custom operator new called.
Normal operator new called.
Custom operator delete called.
Normal operator delete called.
exception 请按任意键继续. . .
*/ #include <cstddef>
#include <new>
#include <iostream>
using namespace std; void * operator new(std::size_t sz)
throw(std::bad_alloc)
{
std::cout << "Normal operator new called." << std::endl ; void * p = std::malloc(sz) ;
if (!p)
throw std::bad_alloc() ;
return p ;
} void operator delete(void * p) throw()
{
std::cout << "Normal operator delete called." << std::endl ;
if (p)
std::free(p) ;
} void * operator new(std::size_t sz, std::ostream & out)
throw(std::bad_alloc)
{
out << "Custom operator new called." << std::endl ;
return ::operator new(sz) ;
} void operator delete(void * p, std::ostream & out) throw()
{
out << "Custom operator delete called." << std::endl ;
::operator delete(p) ;
} class T
{
public:
T(bool should_throw) { if (should_throw) throw 1 ; }
} ; int main()
{
// Calls normal new, normal delete.调用标准的new、delete
T * p = new T(false) ;
delete p ;
std::cout << std::endl ; // Calls custom new, normal delete. 调用自定义的new, 标准的delete
//调用规则 new(A,B) class(param) -> operator new(sizeof(class), A, B)
p = new(std::cout) T(false) ;
delete p ;
std::cout << std::endl ; // Calls normal new, normal delete. 调用标准的new、delete 而且抛出异常
try
{
T * p = new T(true) ;
cout << "accident happens, should not get here" << endl;
//上面语句,没打印,说明不是代码调用了delete,而是c++框架自动为我们掉delete,即new失败时系统会自动为我们调用delete函数
delete p ;
}
catch (...)
{
cout << "exception" << endl;
}
std::cout << std::endl ; // Calls custom new, custom delete. 调用自定义的new、delete,这个使用我们自定义的delete会得到调用
// 规则是 new(X, Y) class(param); -> delete(X, Y) class;
try
{
T * p = new(std::cout) T(true) ;
cout << "accident happens, should not get here" << endl;
delete p ;
}
catch (...)
{
cout << "exception" << endl;
}
std::cout << std::endl ;
}

重载new delete操作符是怎么调用的的更多相关文章

  1. C++ 重载new和delete操作符

    原因: C++标准库提供的new和delete操作符,是一个通用实现,未针对具体对象做具体分析 存在分配器速度慢.小型对象空间浪费严重等问题,不适用于对效率和内存有限制的应用场景   好处: 灵活的内 ...

  2. C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象

    重载操作符与转换 --调用操作符和函数对象 引言: 能够为类类型的对象重载函数调用操作符:一般为表示操作的类重载调用操作符! struct absInt { int operator() (int v ...

  3. C++学习笔记13:运算符重载(赋值操作符2)

    移动语义 完成所有权的移交,当拷贝构造和赋值构造时,目标对象的所有权必须移交给我们的新的对象,原始对象将丧失所有权,_p指针将不再指向原来的那个数组: 左值与右值 C原始定义 左值:可以出现在赋值号的 ...

  4. javascript之delete操作符

    理解delete 理论 代码段的类型 执行上下文 活动对象 / 变量对象 属性的特性 内置属性与 DontDelete 未声明的赋值 Firebug的困惑 在eval中删除变量 浏览器兼容性 Geck ...

  5. C#中如何利用操作符重载和转换操作符

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  6. C#中如何利用操作符重载和转换操作符 (转载)

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  7. C++ new delete操作符

    //new delete操作符 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; /* 1.n ...

  8. js中的内部属性与delete操作符

    本文正式地址:http://www.xiabingbao.com/javascript/2015/08/03/javascript-delete-configurable 在讲解Configurabl ...

  9. C++语言基础(2)-new和delete操作符

    在C语言中,动态分配内存用 malloc() 函数,释放内存用 free() 函数.如下所示: ); //分配10个int型的内存空间 free(p); //释放内存 在C++中,这两个函数仍然可以使 ...

随机推荐

  1. 暂时和永久改动oracle sysdate的默认输出格式

    1.当前会话有效 alter session set NLS_DATE_FORMAT='YYYY-MM-DD:HH24:MI:SS'; 2.永久生效 sys用户登入后运行例如以下命令 然后重新启动数据 ...

  2. reason: 'unable to dequeue a cell with identifier Cell

    今天在cell重用的时候出现一下错误 reason:  'unable  to  dequeue  a  cell  with  identifier  Cell  -  must  register ...

  3. gpexpand error:Do not have enough valid segments to start the array.

    gpstart error: Do not have enough valid segments to start the array. 这个时候需要检查一下shared_buffers设置改小点,就 ...

  4. Pyton——int内部功能介绍

    int内部功能详解: class int(object): """ int(x=0) -> integer int(x, base=10) -> intege ...

  5. Python 2.7 学习笔记 条件与循环语句

    本文介绍下python条件和循环语句的语法 一.if条件语句 语法格式如下: if 表达式: .... elif 表达式: .... elif 表达式: .... else: ..... 说明:与其它 ...

  6. perl 安装 ZooKeeper模块

    1072 ./configure --libdir=/usr/lib 1073 make 1074 make install 1075 cpan ZooKeeper [root@wx03 c]# pe ...

  7. python 字符串处理

    介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作 什么是字符串 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s= ...

  8. iOS开发- 获取精确剩余电量

    [UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice currentDevic ...

  9. B - 队列,推荐

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Desc ...

  10. 【转】C++类-内存分布

    C++类内存分布 - 转载自Jerry19880126 - 博客园 的文章 在上面这篇文章的基础上做了些整理. 主要讨论了C++类对象的内存分布结构. 来看看编译器是怎么处理类成员内存分布的,特别是在 ...