C++ 内存分配(new,operator new)面面观 (转)
本文主要讲述C++ new运算符和operator new, placement new之间的种种关联,new的底层实现,以及operator new的重载和一些在内存池,STL中的应用。
一 new运算符和operator new():
operator new can be called explicitly as a regular function, but in C++, new is an operator with a very specific behavior: An expression with the new operator, first calls function operator new (i.e., this function) with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed). Finally, the expression evaluates as a pointer to the appropriate type.- <span style="font-family:SimHei;font-size:14px;">//平台:Visual Stdio 2008
- #include<iostream>
- class A
- {
- public:
- A()
- {
- std::cout<<"call A constructor"<<std::endl;
- }
- ~A()
- {
- std::cout<<"call A destructor"<<std::endl;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- A* a = new A;
- delete a;
- system("pause");
- return 0;
- }</span>
下面我们跟踪一下A反汇编代码,由于Debug版本反汇编跳转太多,因此此处通过Release版本在A* a = new A;处设断点反汇编:
- <span style="font-family:SimHei;font-size:14px;"> A* a = new A;
- 01301022 push 1 ;不含数据成员的类占用一字节空间,此处压入sizeof(A)
- 01301024 call operator new (13013C2h) ;调用operator new(size_t size)
- 01301029 mov esi,eax ;返回值保存到esi
- 0130102B add esp,4 ;平衡栈
- 0130102E mov dword ptr [esp+8],esi ;
- 01301032 mov dword ptr [esp+14h],0
- 0130103A test esi,esi ;在operator new之后,检查其返回值,如果为空(分配失败),则不调用A()构造函数
- 0130103C je wmain+62h (1301062h) ;为空 跳过构造函数部分
- 0130103E mov eax,dword ptr [__imp_std::endl (1302038h)] ;构造函数内部,输出字符串
- 01301043 mov ecx,dword ptr [__imp_std::cout (1302050h)]
- 01301049 push eax
- 0130104A push offset string "call A constructor" (1302134h)
- 0130104F push ecx
- 01301050 call std::operator<<<std::char_traits<char> > (13011F0h)
- 01301055 add esp,8
- 01301058 mov ecx,eax
- 0130105A call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1302040h)]
- 01301060 jmp wmain+64h (1301064h) ;构造完成,跳过下一句
- 01301062 xor esi,esi ;将esi置空,这里的esi即为new A的返回值
- 01301064 mov dword ptr [esp+14h],0FFFFFFFFh
- delete a;
- 0130106C test esi,esi ;检查a是否为空
- 0130106E je wmain+9Bh (130109Bh) ;如果为空,跳过析构函数和operator delete
- 01301070 mov edx,dword ptr [__imp_std::endl (1302038h)] ;析构函数 输出字符串
- 01301076 mov eax,dword ptr [__imp_std::cout (1302050h)]
- 0130107B push edx
- 0130107C push offset string "call A destructor" (1302148h)
- 01301081 push eax
- 01301082 call std::operator<<<std::char_traits<char> > (13011F0h)
- 01301087 add esp,8
- 0130108A mov ecx,eax
- 0130108C call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1302040h)]
- 01301092 push esi ;压入a
- 01301093 call operator delete (13013BCh) ;调用operator delete
- 01301098 add esp,4
- 通过反汇编可以看出A* = new A包含了operator new(sizeof(A))和A()两个步骤(当然,最后还要将值返回到a)
- delete a包含了~A()和operator delete(a)两个步骤。
- </span>
二 operator new的三种形式:
| throwing (1) |
void* operator new (std::size_t size) throw (std::bad_alloc); |
|---|---|
| nothrow (2) |
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw(); |
| placement (3) |
void* operator new (std::size_t size, void* ptr) throw(); |
- <span style="font-family:Microsoft YaHei;font-size:14px;">#ifndef __PLACEMENT_NEW_INLINE
- #define __PLACEMENT_NEW_INLINE
- inline void *__cdecl operator new(size_t, void *_P)
- {return (_P); }
- #if _MSC_VER >= 1200
- inline void __cdecl operator delete(void *, void *)
- {return; }
- #endif
- #endif</span>
- <span style="font-family:SimHei;font-size:14px;">#include <iostream>
- class A
- {
- public:
- A()
- {
- std::cout<<"call A constructor"<<std::endl;
- }
- ~A()
- {
- std::cout<<"call A destructor"<<std::endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- A* p = (A*)::operator new(sizeof(A)); //分配
- new(p) A(); //构造
- p->~A(); //析构
- ::operator delete(p); //释放
- system("pause");
- return 0;
- }</span>
- <span style="font-family:SimHei;font-size:14px;"> A* a = (A*)::operator new(sizeof(A)); //分配
- 00F9151D push 1
- 00F9151F call operator new (0F91208h) ;调用::operator new(size_t size)也就是throwing(1)版本
- 00F91524 add esp,4
- 00F91527 mov dword ptr [ebp-14h],eax ;返回地址放入[ebp-14h] 即为p
- new(a) A(); //构造
- 00F9152A mov eax,dword ptr [ebp-14h]
- 00F9152D push eax
- 00F9152E push 1 ;压入p
- 00F91530 call operator new (0F91280h);调用operator new(size_t, void* p)即placement(3)版本 只是简单返回p
- 00F91535 add esp,8
- 00F91538 mov dword ptr [ebp-0E0h],eax ;将p放入[ebp-0E0h]
- 00F9153E mov dword ptr [ebp-4],0
- 00F91545 cmp dword ptr [ebp-0E0h],0 ;判断p是否为空
- 00F9154C je wmain+81h (0F91561h) ;如果为空 跳过构造函数
- 00F9154E mov ecx,dword ptr [ebp-0E0h] ;取出p到ecx
- 00F91554 call A::A (0F91285h) ;调用构造函数 根据_thiscall调用约定 this指针通过ecx寄存器传递
- 00F91559 mov dword ptr [ebp-0F4h],eax ;将返回值(this指针)放入[ebp-0F4h]中
- 00F9155F jmp wmain+8Bh (0F9156Bh) ;跳过下一句
- 00F91561 mov dword ptr [ebp-0F4h],0 ;将[ebp-0F4h]置空 当前面判断p为空时执行此语句
- 00F9156B mov ecx,dword ptr [ebp-0F4h] ;[ebp-0F4h]为最终构造完成后的this指针(或者为空) 放入ecx
- 00F91571 mov dword ptr [ebp-0ECh],ecx ;又将this放入[ebp-0ECh] 这些都是调试所用
- 00F91577 mov dword ptr [ebp-4],0FFFFFFFFh
- a->~A(); //析构
- 00F9157E push 0
- 00F91580 mov ecx,dword ptr [ebp-14h] ;从[ebp-14h]中取出p
- 00F91583 call A::`scalar deleting destructor' (0F91041h) ;调用析构函数(跟踪进去比较复杂 如果在Release下,构造析构函数都是直接展开的)
- ::operator delete(a); //释放
- 00F91588 mov eax,dword ptr [ebp-14h] ;将p放入eax
- 00F9158B push eax ;压入p
- 00F9158C call operator delete (0F910B9h);调用operator delete(void* )
- 00F91591 add esp,4 </span>
- <span style="font-family:SimHei;font-size:14px;"> A* a = (A*)::operator new(sizeof(A)); //分配
- 010614FE push 1
- 01061500 call operator new (1061208h)
- 01061505 add esp,4
- 01061508 mov dword ptr [a],eax
- //new(a) A(); //构造
- a->A::A();
- 0106150B mov ecx,dword ptr [a]
- 0106150E call operator new (1061285h)
- a->~A(); //析构
- 01061513 push 0
- 01061515 mov ecx,dword ptr [a]
- 01061518 call A::`scalar deleting destructor' (1061041h)
- ::operator delete(a); //释放
- 0106151D mov eax,dword ptr [a]
- 01061520 push eax
- 01061521 call operator delete (10610B9h)
- 01061526 add esp,4 </span>
三 operator new重载:
1.在类中重载operator new
- <span style="font-family:SimHei;font-size:14px;">#include <iostream>
- class A
- {
- public:
- A()
- {
- std::cout<<"call A constructor"<<std::endl;
- }
- ~A()
- {
- std::cout<<"call A destructor"<<std::endl;
- }
- void* operator new(size_t size)
- {
- std::cout<<"call A::operator new"<<std::endl;
- return malloc(size);
- }
- void* operator new(size_t size, const std::nothrow_t& nothrow_value)
- {
- std::cout<<"call A::operator new nothrow"<<std::endl;
- return malloc(size);
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- A* p1 = new A;
- delete p1;
- A* p2 = new(std::nothrow) A;
- delete p2;
- system("pause");
- return 0;
- }</span>
- <span style="font-family:SimHei;font-size:14px;">void* ::operator new(size_t size)
- {
- std::cout<<"call global operator new"<<std::endl;
- return malloc(size);
- }</span>
error C2660: “A::operator new”: 函数不接受 2 个参数。
- <span style="font-family:Microsoft YaHei;font-size:14px;">void* operator new(size_t size, int x, int y, int z)
- {
- std::cout<<"X="<<x<<" Y="<<y<<" Z="<<z<<std::endl;
- return malloc(size);
- }</span>
2.重载全局operator new
3.类中operator new和全局operator new的调用时机
四 operator new运用技巧和一些实例探索
1.operator new重载运用于调试:
- <span style="font-family:SimHei;font-size:14px;">//A.h
- class A
- {
- public:
- A()
- {
- std::cout<<"call A constructor"<<std::endl;
- }
- ~A()
- {
- std::cout<<"call A destructor"<<std::endl;
- }
- void* operator new(size_t size, const char* file, int line)
- {
- std::cout<<"call A::operator new on file:"<<file<<" line:"<<line<<std::endl;
- return malloc(size);
- return NULL;
- }
- };
- //Test.cpp
- #include <iostream>
- #include "A.h"
- #define new new(__FILE__, __LINE__)
- int _tmain(int argc, _TCHAR* argv[])
- {
- A* p1 = new A;
- delete p1;
- A* p2 = new A;
- delete p2;
- system("pause");
- return 0;
- }</span>
2.内存池优化
3.STL中的new
五 delete的使用
- <span style="font-family:SimHei;font-size:14px;">void* operator new(size_t size, int x)
- {
- cout<<" x = "<<x<<endl;
- return malloc(size);
- }
- void operator delete(void* p, int x)
- {
- cout<<" x = "<<x<<endl;
- free(p);
- }</span>
六 关于new和内存分配的其他
1.set_new_handler
- <span style="font-family:SimHei;font-size:14px;">#include <iostream>
- #include <new.h>// 使用_set_new_mode和set_new_handler
- void nomem_handler()
- {
- std::cout<<"call nomem_handler"<<std::endl;
- }
- int main()
- {
- _set_new_mode(1); //使new_handler有效
- set_new_handler(nomem_handler);//指定入口函数 函数原型void f();
- std::cout<<"try to alloc 2GB memory...."<<std::endl;
- char* a = (char*)malloc(2*1024*1024*1024);
- if(a)
- std::cout<<"ok...I got it"<<std::endl;
- free(a);
- system("pause");
- }</span>
2.new分配数组
- <span style="font-family:SimHei;font-size:14px;">//A.h
- class A
- {
- public:
- A()
- {
- std::cout<<"call A constructor"<<std::endl;
- }
- ~A()
- {
- std::cout<<"call A destructor"<<std::endl;
- }
- void* operator new(size_t size)
- {
- std::cout<<"call A::operator new[] size:"<<size<<std::endl;
- return malloc(size);
- }
- void operator delete[](void* p)
- {
- std::cout<<"call A::operator delete[]"<<std::endl;
- free(p);
- }
- void operator delete(void* p)
- {
- free(p);
- }
- };</span>
- <span style="font-family:SimHei;font-size:14px;">//Test.cpp
- #include <iostream>
- #include "A.h"
- void* operator new[](size_t size)
- {
- std::cout<<"call global new[] size: "<<size<<std::endl;
- return malloc(size);
- }
- void operator delete[](void* p)
- {
- std::cout<<"call global delete[] "<<std::endl;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- std::cout<<"sizeof A "<<sizeof(A)<<std::endl;
- A* p1 = new A[3];
- delete []p1;
- system("pause");
- return 0;
- }</span>
简单跟踪了一下:
参考文献:
C++ 内存分配(new,operator new)面面观 (转)的更多相关文章
- 【转】C++ 内存分配(new,operator new)详解
本文主要讲述C++ new运算符和operator new, placement new之间的种种关联,new的底层实现,以及operator new的重载和一些在内存池,STL中的应用. 一 new ...
- 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new
引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...
- C++ 内存分配(new,operator new)详解
参考:C++ 内存分配(new,operator new)详解 如何限制对象只能建立在堆上或者栈上 new运算符和operator new() new:指我们在C++里通常用到的运算符,比如A* a ...
- 源码分析:Java对象的内存分配
Java对象的分配,根据其过程,将其分为快速分配和慢速分配两种形式,其中快速分配使用无锁的指针碰撞技术在新生代的Eden区上进行分配,而慢速分配根据堆的实现方式.GC的实现方式.代的实现方式不同而具有 ...
- C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的变量通常是局部变量.函数参数等.在一个进程中,位于用户虚拟地址空间顶部的是用户栈,编译器用它来实现函数的调用.和堆一样 ...
- 【转载】C++内存分配
原文:C++内存分配 内存泄露相信对C++程序员来说都不陌生.解决内存泄露的方案多种多样,大部分方案以追踪检测为主,这种方法实现起来容易,使用方便,也比较安全. 首先我们要确定这个模块的主要功能: 能 ...
- 转:内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]
内存区划分.内存分配.常量存储区.堆.栈.自由存储区.全局区[C++][内存管理][转载] 一. 在c中分为这几个存储区1.栈 - 由编译器自动分配释放2.堆 - 一般由程序员分配释放,若程序员不释放 ...
- 转:c++内存分配
第一篇: http://my.oschina.net/pollybl1255/blog/140323 BSS段:(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS ...
- 内存分配(c/c++)
C++中内存分配 内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 1,栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清除的变量的存储区.里面的 ...
随机推荐
- python网络编程(进程与多线程)
multiprocessing模块 由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiproce ...
- linux下 ls -l 命令显示结果每一列代表什么意思
第一个栏位,表示文件的属性.Linux的文件基本上分为三个属性:可读(r),可写(w),可执行(x).但是这里有十个格子可以添(具体程序实现时,实际上是十个bit位).第一个小格是特殊表示格,表示目录 ...
- nginx服务器配置/websocket nginx 配置笔记
server { listen 80; server_name xxx.com; # add_header '*' ; location /u/ { # 反向代理透传客户端ip proxy_set_h ...
- win7、win10进程pid4占用80端口的解决办法
https://jingyan.baidu.com/article/7e4409533ffe092fc1e2ef10.html 今天想用wamp架设服务器,但是程序启动不起来,查看系统端口,80端口被 ...
- 浅析RPC概念框架
本文原封不动的来至于csdn MindWind,原文请见 RPC:RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一 ...
- servlet入门学习之API
java servlet API学习网址: http://tomcat.apache.org/tomcat-7.0-doc/servletapi/ http://tomcat.apache.org/t ...
- Java Servlet API中文说明文档
Java Servlet API中文说明文档 目 录 1.... Servet资料 1.1 绪言 1.2 谁需要读这份文档 1.3 Java Servlet API的组成 ...
- shell实例练习+详解
想着将Shell与Python和Java等脚本比较比较,当一有这个念头我就放弃了.这太侮辱Shell了.(哭笑脸!) 作为一个程序员,Linux那是最基本要求.而shell脚本有时候也会显示它在Lin ...
- Java进阶篇(六)——Swing程序设计(下)
三.布局管理器 Swing中,每个组件在容器中都有一个具体的位置和大小,在容器中摆放各自组件时很难判断其具体位置和大小,这里我们就要引入布局管理器了,它提供了基本的布局功能,可以有效的处理整个窗体的布 ...
- Python中几种数据类型list, tuple,dict,set的使用演示
还是直接上代码,看着代码运行,看函数介绍 # coding=utf-8 # 1 list-列表 的用法 students = [1,2,3] a = 5 classmates = [students* ...