C++汇编实现分析__1

本文通过对C++代码的汇编分析,详细解释了`new`和`delete`操作符在VS2005 Release模式下不开启优化时的底层实现。内容包括函数栈的设置、异常处理、内存分配、字符串构造和析构、以及内存释放的过程。通过对汇编代码的解读,揭示了C++内存管理的细节。
摘要由CSDN通过智能技术生成
int _tmain(int argc, _TCHAR* argv[])
{
    string *pStr = new string ("abc");

    delete pStr;

    return 0;
}

VS2005,Release,优化关闭时的汇编代码

int _tmain(int argc, _TCHAR* argv[])
{
00401B40  push        ebp  
00401B41  mov         ebp,esp
00401B43  push        0FFFFFFFFh
00401B45  push        offset __ehhandler$?ThreadFunc1@@YGKPAX@Z+29h (401D99h)
00401B4A  mov         eax,dword ptr fs:[00000000h]
00401B50  push        eax  
00401B51  sub         esp,1Ch
00401B54  mov         eax,dword ptr [___security_cookie (403004h)]
00401B59  xor         eax,ebp
00401B5B  push        eax  
00401B5C  lea         eax,[ebp-0Ch]
00401B5F  mov         dword ptr fs:[00000000h],eax
    string *pStr = new string ("abc");
00401B65  push        1Ch  
00401B67  call        operator new (40112Eh)
00401B6C  add         esp,4
00401B6F  mov         dword ptr [ebp-18h],eax
00401B72  mov         dword ptr [ebp-4],0
00401B79  cmp         dword ptr [ebp-18h],0
00401B7D  je          main+52h (401B92h)
00401B7F  push        4025ACh
00401B84  mov         ecx,dword ptr [ebp-18h]
00401B87  call        dword ptr [__imp_std::basic_string ,std::allocator >::basic_string ,std::allocator > (402050h)]
00401B8D  mov         dword ptr [ebp-24h],eax
00401B90  jmp         main+59h (401B99h)
00401B92  mov         dword ptr [ebp-24h],0
00401B99  mov         eax,dword ptr [ebp-24h]
00401B9C  mov         dword ptr [ebp-14h],eax
00401B9F  mov         dword ptr [ebp-4],0FFFFFFFFh
00401BA6  mov         ecx,dword ptr [ebp-14h]
00401BA9  mov         dword ptr [ebp-10h],ecx

    delete pStr;
00401BAC  mov         edx,dword ptr [ebp-10h]
00401BAF  mov         dword ptr [ebp-20h],edx
00401BB2  mov         eax,dword ptr [ebp-20h]
00401BB5  mov         dword ptr [ebp-1Ch],eax
00401BB8  cmp         dword ptr [ebp-1Ch],0
00401BBC  je          main+8Dh (401BCDh)
00401BBE  push        1    
00401BC0  mov         ecx,dword ptr [ebp-1Ch]
00401BC3  call        std::basic_string ,std::allocator >::`scalar deleting destructor' (4010E0h)
00401BC8  mov         dword ptr [ebp-28h],eax
00401BCB  jmp         main+94h (401BD4h)
00401BCD  mov         dword ptr [ebp-28h],0

    return 0;
00401BD4  xor         eax,eax
}
00401BD6  mov         ecx,dword ptr [ebp-0Ch]
00401BD
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值