operator new和operator delete
从STL源码剖析中看到了operator new的使用
template<class T>
inline void _deallocate(T* buffer) {
::operator delete(buffer);
//operator delete可以被重载
// operator delete(buffer);
}
从而开始研究一下这两个操作符
首先其实迷惑的是"::"的作用,通过以下代码测试出来了
class A {
public:
void TT() {
cout << "A" << endl;
}
};
inline void TT() {
cout<<"G"<<endl;
}
class B: public A {
public:
void TT() {
cout << "B" << endl;
::TT();
}
};
int main(int argc, char **argv) {
// new Allocator();
(new B())->TT();
return 0;
}
运行结果
B
G
用于区分全局函数和函数内局部函数的符号。
#include <iostream>
#include <vector>
#include "2jjalloca.h"
#include <new> using namespace std; //inline void *operator new(size_t n) {
// cout << "global new" << endl;
// return ::operator new(n);
//}
//
//inline void *operator new(size_t n, const std::nothrow_t& nt) {
// cout << "global new nothrow" << endl;
// return ::operator new(n, nt);
//} class Allocator {
public:
void *operator new(size_t n) {
cout << "allocator new" << endl;
return ::operator new(n);
}
//作用域覆盖原则,即在里向外寻找operator new的重载时,
//只要找到operator new()函数就不再向外查找,如果参数符合则通过,
//如果参数不符合则报错,而不管全局是否还有相匹配的函数原型。
//所以的调用new(std::nothrow) Allocator;注释这个函数是报错
void* operator new(size_t n, const std::nothrow_t& nt) {
cout << "allocator new nothrow" << endl;
return ::operator new(n, nt);
} void* operator new(size_t n, void *p) {
cout << "allocator placement new" << endl;
return p;
} void operator delete(void *p) {
cout << "allocator delete" << endl;
return ::operator delete(p);
}
void operator delete(void*, void*) {
cout << "allocator placement delete" << endl;
} void* operator new[](size_t n) {
cout << "allocator new[]" << endl;
return ::operator new[](n);
}
}; int main(int argc, char **argv) {
// Allocator *a = new Allocator;
// delete a;
// ::new Allcator; Allocator *a=new Allocator[10];
return 0;
}
参考文章
C++ 内存分配(new,operator new)详解 (有些和我尝试的不一样,第四节可以着重参考)
operator new和operator delete的更多相关文章
- Effective C++ 第二版 8) 写operator new 和operator delete 9) 避免隐藏标准形式的new
条款8 写operator new 和operator delete 时要遵循常规 重写operator new时, 函数提供的行为要和系统缺省的operator new一致: 1)正确的返回值; 2 ...
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器
先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...
- 定制自己的new和delete:operator new 和 operator delete
new和delete不同用法 基本用法 int * aptr = new int(10); delete aptr, aptr = nullptr; 上面的代码是我们最基本也是最常见的使用new和de ...
- C++面向对象高级编程(九)Reference与重载operator new和operator delete
摘要: 技术在于交流.沟通,转载请注明出处并保持作品的完整性. 一 Reference 引用:之前提及过,他的主要作用就是取别名,与指针很相似,实现也是基于指针. 1.引用必须有初值,且不能引用nul ...
- operator new 和 operator delete 实现一个简单内存泄漏跟踪器
先来说下实现思路:可以实现一个Trace类,调用 operator new 的时候就将指向分配内存的指针.当前文件.当前行等信息添加进Trace 成员map容器内,在调用operator delete ...
- 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名(); C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- ZT 自定义operator new与operator delete的使用(1)
http://blog.csdn.net/waken_ma/article/details/4004972 先转两篇文章: 拨开自定义operator new与operator delete的迷雾 C ...
- 类的operator new与operator delete的重载【转】
http://www.cnblogs.com/luxiaoxun/archive/2012/08/11/2633423.html 为什么有必要写自己的operator new和operator del ...
- 条款八: 写operator new和operator delete时要遵循常规
自己重写operator new时(条款10解释了为什么有时要重写它),很重要的一点是函数提供的行为要和系统缺省的operator new一致.实际做起来也就是:要有正确的返回值:可用内存不够时要调用 ...
随机推荐
- mysql将视图数据迁移到表中
#字段必须完全一样 INSERT into table1(所有字段) select * from data.视图
- QTP自动化测试-笔记 注释、大小写
1 rem 注释内容 2 ' 注释内容 3 快捷键注释-选择代码行-ctrl+M 4 ctrl+shift+同- 取消注释 大小写 qtp:对小写敏感:如果 变量.sheet页是用小写字母命名,则使用 ...
- epoch、 iteration和batchsize区别
转自: https://blog.csdn.net/qq_27923041/article/details/74927398 深度学习中经常看到epoch. iteration和batchsize,下 ...
- redis.clients.jedis.exceptions.JedisDataException :READONLY You can't write
分布式直连同步调用测试时出现的错误:主从复制架构下,默认Slave是只读的,如果写入则会报错: redis.clients.jedis.exceptions.JedisDataException: R ...
- MyISAM索引和InnoDB索引的区别
首先你要知道: 无论是Myisam和Innodb引擎,如果在建表的时候没有显示的定义一行主键列的话,他内部都会自动创建一个隐藏的主键索引: 主键索引以外的索引假设称为次索引:首先Myisam和Inno ...
- java 运行 .jar 文件乱码
http://yang3wei.github.io/blog/2013/02/10/java-dfile-dot-encoding-equals-utf-8-gan-diao-luan-ma/ 启动时 ...
- ADO.NET工具类(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- CML\LVDS
LVDS和CML电平应用区别 https://wenku.baidu.com/view/6bb0066f011ca300a6c3908c.html
- Lisp小程序,大作用,不该放弃!
从听说autolisp到现在已经20年了, 学了一点点, 可惜中间没能坚持下来, 放弃了! 今天在画图, 图纸是从revit转成dwg的, 其中有些文本的朝向是错误的, 如果手工旋转很是费事, ...
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...