new和delete时究竟发生了什么?
2007-05-28 09:22
302 查看
在C++中,当用户调用new和delete时,实际上分别发生了如下事情:
new:
1. 调用::operator new(相当于malloc), 分配一块内存;
2. 检查第一步内存分配的结果,如果未被正常分配(比如系统资源耗尽时),抛出异常,直接返回,接下来的步骤不会执行;
3. 调用构造函数.
delete:
1. 检查指定要删除的内存,如果已经为空,直接返回,接下来的步骤不会执行;
2. 调用析构函数;
3. 调用::operator delete(相当于free),释放内存.
下面我们通过查看测试代码的相应汇编代码来证实上述观点.
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MyClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MyClass(const char*) ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~MyClass() ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
operator char() ...{return 'a';}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char* m_data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* my = new MyClass("Hello!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete my;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char memory[sizeof(MyClass)];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void* p = memory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* second = new (p) MyClass("World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
second->~MyClass();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相应的汇编代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004136E0 push ebp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
……
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* my = new MyClass("Hello!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041371D push 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041371F call operator new (411190h) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413724 add esp,4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413727 mov dword ptr [ebp-134h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041372D mov dword ptr [ebp-4],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413734 cmp dword ptr [ebp-134h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041373B je wmain+75h (413755h) // 步骤2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041373D push offset string "Hello!" (41676Ch)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413742 mov ecx,dword ptr [ebp-134h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413748 call MyClass::MyClass (41116Dh) // 步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041374D mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413753 jmp wmain+7Fh (41375Fh)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413755 mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041375F mov eax,dword ptr [ebp-148h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413765 mov dword ptr [ebp-140h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041376B mov dword ptr [ebp-4],0FFFFFFFFh
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413772 mov ecx,dword ptr [ebp-140h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413778 mov dword ptr [ebp-14h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete my;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041377B mov eax,dword ptr [ebp-14h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041377E mov dword ptr [ebp-11Ch],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413784 mov ecx,dword ptr [ebp-11Ch]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041378A mov dword ptr [ebp-128h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413790 cmp dword ptr [ebp-128h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413797 je wmain+0CEh (4137AEh) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413799 push 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041379B mov ecx,dword ptr [ebp-128h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137A1 call MyClass::`scalar deleting destructor' (41100Ah) // 步骤2和步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137A6 mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137AC jmp wmain+0D8h (4137B8h)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137AE mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char memory[sizeof(MyClass)];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void* p = memory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137B8 lea eax,[ebp-20h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137BB mov dword ptr [ebp-2Ch],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* second = new (p) MyClass("World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137BE mov eax,dword ptr [ebp-2Ch]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C1 push eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C2 push 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C4 call operator new (4111E0h) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C9 add esp,8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137CC mov dword ptr [ebp-104h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137D2 mov dword ptr [ebp-4],1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137D9 cmp dword ptr [ebp-104h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E0 je wmain+11Ah (4137FAh) // 步骤2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E2 push offset string "World!" (4166FCh)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E7 mov ecx,dword ptr [ebp-104h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137ED call MyClass::MyClass (41116Dh) // 步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137F2 mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137F8 jmp wmain+124h (413804h)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137FA mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413804 mov ecx,dword ptr [ebp-148h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041380A mov dword ptr [ebp-110h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413810 mov dword ptr [ebp-4],0FFFFFFFFh
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413817 mov edx,dword ptr [ebp-110h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041381D mov dword ptr [ebp-38h],edx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
second->~MyClass();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413820 push 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413822 mov ecx,dword ptr [ebp-38h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413825 call MyClass::`scalar deleting destructor' (41100Ah)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041382A xor eax,eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0041382C push edx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
……
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这里需要特别注意的是:
1. 定位创建(placement new)的方法, 即new(p) MyClass("Hello"),不管p是否被正确创建,定位创建的过程与一般创建的过程是一样的,即也经历3个主要步骤.
2. 汇编代码中所说的scalar deleting destructor是编译器自动生成的, 相当于先执行析构函数再执行delete操作,相关信息可参考http://www.thescripts.com/forum/thread263644.html
new:
1. 调用::operator new(相当于malloc), 分配一块内存;
2. 检查第一步内存分配的结果,如果未被正常分配(比如系统资源耗尽时),抛出异常,直接返回,接下来的步骤不会执行;
3. 调用构造函数.
delete:
1. 检查指定要删除的内存,如果已经为空,直接返回,接下来的步骤不会执行;
2. 调用析构函数;
3. 调用::operator delete(相当于free),释放内存.
下面我们通过查看测试代码的相应汇编代码来证实上述观点.
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include "stdafx.h"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <iostream>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class MyClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
MyClass(const char*) ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~MyClass() ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
operator char() ...{return 'a';}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char* m_data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* my = new MyClass("Hello!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete my;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char memory[sizeof(MyClass)];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void* p = memory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* second = new (p) MyClass("World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
second->~MyClass();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相应的汇编代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004136E0 push ebp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
……
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* my = new MyClass("Hello!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041371D push 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041371F call operator new (411190h) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413724 add esp,4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413727 mov dword ptr [ebp-134h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041372D mov dword ptr [ebp-4],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413734 cmp dword ptr [ebp-134h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041373B je wmain+75h (413755h) // 步骤2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041373D push offset string "Hello!" (41676Ch)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413742 mov ecx,dword ptr [ebp-134h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413748 call MyClass::MyClass (41116Dh) // 步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041374D mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413753 jmp wmain+7Fh (41375Fh)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413755 mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041375F mov eax,dword ptr [ebp-148h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413765 mov dword ptr [ebp-140h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041376B mov dword ptr [ebp-4],0FFFFFFFFh
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413772 mov ecx,dword ptr [ebp-140h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413778 mov dword ptr [ebp-14h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete my;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041377B mov eax,dword ptr [ebp-14h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041377E mov dword ptr [ebp-11Ch],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413784 mov ecx,dword ptr [ebp-11Ch]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041378A mov dword ptr [ebp-128h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413790 cmp dword ptr [ebp-128h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413797 je wmain+0CEh (4137AEh) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413799 push 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041379B mov ecx,dword ptr [ebp-128h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137A1 call MyClass::`scalar deleting destructor' (41100Ah) // 步骤2和步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137A6 mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137AC jmp wmain+0D8h (4137B8h)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137AE mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char memory[sizeof(MyClass)];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void* p = memory;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137B8 lea eax,[ebp-20h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137BB mov dword ptr [ebp-2Ch],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MyClass* second = new (p) MyClass("World!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137BE mov eax,dword ptr [ebp-2Ch]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C1 push eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C2 push 4
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C4 call operator new (4111E0h) // 步骤1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137C9 add esp,8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137CC mov dword ptr [ebp-104h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137D2 mov dword ptr [ebp-4],1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137D9 cmp dword ptr [ebp-104h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E0 je wmain+11Ah (4137FAh) // 步骤2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E2 push offset string "World!" (4166FCh)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137E7 mov ecx,dword ptr [ebp-104h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137ED call MyClass::MyClass (41116Dh) // 步骤3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137F2 mov dword ptr [ebp-148h],eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137F8 jmp wmain+124h (413804h)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
004137FA mov dword ptr [ebp-148h],0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413804 mov ecx,dword ptr [ebp-148h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041380A mov dword ptr [ebp-110h],ecx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413810 mov dword ptr [ebp-4],0FFFFFFFFh
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413817 mov edx,dword ptr [ebp-110h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041381D mov dword ptr [ebp-38h],edx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
second->~MyClass();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413820 push 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413822 mov ecx,dword ptr [ebp-38h]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
00413825 call MyClass::`scalar deleting destructor' (41100Ah)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
0041382A xor eax,eax
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
0041382C push edx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
……
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这里需要特别注意的是:
1. 定位创建(placement new)的方法, 即new(p) MyClass("Hello"),不管p是否被正确创建,定位创建的过程与一般创建的过程是一样的,即也经历3个主要步骤.
2. 汇编代码中所说的scalar deleting destructor是编译器自动生成的, 相当于先执行析构函数再执行delete操作,相关信息可参考http://www.thescripts.com/forum/thread263644.html
相关文章推荐
- 调用new/delete时究竟发生了什么----Effective C++学习笔记
- new/delete究竟做了些什么
- public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思
- 当你输入一个网址的时候,究竟发生了什么?
- MySQL主库大表执行delete语句,Ctrl+C具体发生了什么分析
- 输入facebook的URL按下回车后究竟发生了什么
- 输入facebook的URL按下回车后究竟发生了什么
- MySQL数据库下 delete from x where id=?会发生什么锁
- 回滚保存点后究竟发生了什么?
- new一个对象的过程发生了什么
- delete null指针会发生什么
- new/delete 与 malloc/free的区别是什么?
- javascript Object.create()究竟发生了什么
- js中new 一个对象内部发生了什么?
- [转]SharePoint 2010 WSP包部署过程中究竟发生什么?
- 当我们游览网页的时候, 在输入链接点击回车之后到看到新网页这中间究竟发生了什么?
- 当在浏览器地址栏输入一个网址的时候,究竟发生了什么?
- 当在浏览器地址栏输入一个网址的时候,究竟发生了什么?
- new 到底发生了什么
- public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思