惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - 澜心

javascript复习一 JavaScript的面向对象 C#在word中插入上标的问题 新建SSIS项目失败或者在SSIS项目中新建包失败 简单数据库操作 往消息队列传数据的存储过程 C#泛型学习 空气污染指数的计算公式是什么?(API) 行列转换 - 澜心 - 博客园 数据中心和数据仓库,在信息化建设中有何作用? - 澜心 - 博客园 【求助】Vs2005当前不能命中断点 自动加载用户控件问题【找到部分原因】 【讨论】程序员的发展道路如何规划,欢迎大家加入讨论 sql常用函数汇总 网站配色方案学习 用例指南 用例 UseCase 系统分析员基本功 网站安装步骤 sqlserver2000下载地址 SQLServer2000安装图解
面向对象语言的new操作
澜心 · 2011-10-14 · via 博客园 - 澜心

2011-10-14 11:32  澜心  阅读(611)  评论()    收藏  举报

下面是一段简单的C++代码,分下一下new操作符究竟干了一些什么。

  1. class Person  
  2. {  
  3. public:  
  4.     Person(){}  
  5.     ~Person(){}  
  6.     int name;  
  7.     int age;  
  8. };  
  9.   
  10. class son:public Person  
  11. {  
  12. public:  
  13.     son(){}  
  14.     ~son(){}  
  15.     int toy;  
  16. };  
  1. Person *  p= new Person();  
  2. p->name = 44;  
  3. p->age = 30;  
  4.   
  5. Person* s = new son();  
  6. s->name = 44;  

在new的地方,我们按f11跟踪进去。new是封装malloc,按照对象的大小申请内存。我们看到size是8,正好等于person中2个int的大小。

  1. void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)  
  2.         {         
  3.         void *p;  
  4.         while ((p = malloc(size)) == 0)  
  5.                 if (_callnewh(size) == 0)  
  6.                 {         
  7.                 static const std::bad_alloc nomem;  
  8.                 _RAISE(nomem);  
  9.                 }  
  10.   
  11.         return (p);  
  12.         }  

 那malloc又做了些什么操作呢。调用了 dbgmalloc.c 和dbgheap.c.

  1. extern "C" _CRTIMP void * __cdecl malloc (  
  2.         size_t nSize  
  3.         )  
  4. {  
  5.         void *res = _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);  
  6.   
  7.         RTCCALLBACK(_RTC_Allocate_hook, (res, nSize, 0));  
  8.   
  9.         return res;  
  10. }  

  extern "C" void * __cdecl _nh_malloc_dbg (

  1.         size_t nSize,  
  2.         int nhFlag,  
  3.         int nBlockUse,  
  4.         const char * szFileName,  
  5.         int nLine  
  6.         )  
  7. {  
  8.         int errno_tmp = 0;  
  9.         void * pvBlk = _nh_malloc_dbg_impl(nSize, nhFlag, nBlockUse, szFileName, nLine, &errno_tmp);  
  10.   
  11.         if ( pvBlk == NULL && errno_tmp != 0 && _errno())  
  12.         {  
  13.             errno = errno_tmp;   
  14.         }  
  15.         return pvBlk;  
  16. }  
 

没有深究,有时间补上,有研究的童鞋请赐教

参考文章:http://blog.csdn.net/hejinjing_tom_com/article/details/4059711

我们知道new操作要调用对应的构造函数,可是我们在new操作的代码中并没有看到调用构造函数的任何代码,我们被书忽悠了?

那构造函数又是什么时候调用的呢。

请参考  

一件被误导很久的事:关于new和delete

点击debug菜单window中的disassembly,查看汇编代码,很容易找到如下代码

	Person *  p= new Person();
00171860  push        28h  
00171862  call        operator new (17131Bh)  
00171867  add         esp,4  
0017186A  mov         dword ptr [ebp-1A0h],eax  
00171870  mov         dword ptr [ebp-4],1  
00171877  cmp         dword ptr [ebp-1A0h],0  
0017187E  je          test+103h (171893h)  
00171880  mov         ecx,dword ptr [ebp-1A0h]  
00171886  call        Person::Person (1713B6h)  
0017188B  mov         dword ptr [ebp-1CCh],eax  
00171891  jmp         test+10Dh (17189Dh)  
00171893  mov         dword ptr [ebp-1CCh],0  
0017189D  mov         eax,dword ptr [ebp-1CCh]  
001718A3  mov         dword ptr [ebp-1ACh],eax  
001718A9  mov         dword ptr [ebp-4],0FFFFFFFFh  
001718B0  mov         ecx,dword ptr [ebp-1ACh]  
001718B6  mov         dword ptr [ebp-20h],ecx  
	p->name  = "44";

对比C++代码,在  Person * p= new Person(); 和p->name = "44"中间的应该就是new运算符对应的汇编。

对类a的构造函数的调用,是编译器偷偷在你的函数里插入的,当时的情况就是如此。delete的情况也是一摸一样。

 我们分析对构造函数的调用应该是编译器插入的。 至于怎么插入的,现在还不太明白。

参考文章:http://hi.baidu.com/linx214/blog/item/62251ace1f970e35f8dc6116.html

这个没有找到原帖,抱歉。

子类是怎样调用父类的成员函数的 http://blog.csdn.net/wishfly/article/details/5030381

C++:从子类访问父类的私有函数  http://hi.baidu.com/structureit/blog/item/3b041f6d86945af64216940c.html

http://campolake.iteye.com/blog/1195685

原文地址:

http://campolake.iteye.com/blog/1195685