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一致.实际做起来也就是:要有正确的返回值:可用内存不够时要调用 ...
随机推荐
- css元素选择器
css的元素选择器就是html的标签名:
- 检索 COM 类工厂中 CLSID 为 {91493441-5A91-11CF-8700-00AA0060263B} 的组件失败
Symptoms When no user is interactively logged on to the server console, if you try to start a COM+ a ...
- codeforces29A
Spit Problem CodeForces - 29A In a Berland's zoo there is an enclosure with camels. It is known that ...
- 三星Galaxy S10可望率先应用于1TB的手机内存
导读 三星电子(Samsung Electronics)1月30日宣布,已经开始量产业界首款容量高达1TB的嵌入式通用闪存存储器(embedded Universal Flash Storage,eU ...
- 前端部分-CSS基础介绍
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.也就是定义相应的标签语言来定制显示样式达到一定的显示效果. 每个CSS样式由两个组成部分:选择器和 ...
- linux拷贝多个目录下的文件到同一个目录
拷贝a目录下的a.txt和b目录下的b.txt到c目录 cp -a \ /root/a/a.txt \ /root/b/b.txt \ /root/c/
- Mail.Ru Cup 2018 Round 2
A:阅读理解. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- Redis——windows下如何连接Linux(centos7.x)虚拟机的Redis——【二】
我的虚拟网络使用的是桥接网络和windows主机IP为同一网段,做下面步骤之前请确保网络通畅. 使用cmd的ping来测试 软件 https://redisdesktop.com/download 下 ...
- mysql 导入sql 2006 - mysql server has gone away 导入
解决办法:找到你的mysql目录下的my.ini配置文件,加入以下代码 max_allowed_packet=500M wait_timeout=288000 interactive_timeout ...
- CS Academy Sliding Product Sum(组合数)
题意 有一个长为 \(N\) 的序列 \(A = [1, 2, 3, \dots, N]\) ,求所有长度 \(\le K\) 的子串权值积的和,对于 \(M\) 取模. \(N \le 10^{18 ...