从逆向分析角度看C++拷贝构造函数
作者:网络转载 发布时间:[ 2013/3/19 10:28:35 ] 推荐标签:
一段C++代码:
//: HowMany_2.cpp
#include <iostream>
using namespace std;
class HowMany {
static int objectCount;
public:
HowMany() {
++objectCount;
print("HowMany()");
}
~HowMany() {
--objectCount;
print("~HowMany()");
}
HowMany(const HowMany& h) {
++objectCount;
print("HowMany(const HowMany&)");
}
void print(const char ss[]) {
cout << ss << ": ";
cout << "objectCount = " << objectCount << endl;
return ;
}
};
int HowMany::objectCount = 0;
HowMany f(HowMany x) {
x.print("x argument inside f()");
cout << "Return From f()" << endl;
return x; // 有返回值 x
}
int main() {
{
HowMany h;
cout << "Entering f()" << endl;
HowMany h2 = f(h);
}
return 0;
} ///:~
运行结果:
Assembly Code:
38: int main() {
39: {
40: HowMany h;
004017FD lea ecx,[h] ; [h] 为对象 h 的内存地址
00401800 call @ILT+685(HowMany::HowMany) (004012b2) ; 调用构造函数
00401805 mov dword ptr [ebp-4],0
41: cout << "Entering f()" << endl;
0040180C push offset @ILT+200(std::endl) (004010cd)
00401811 push offset string "Entering f()" (0046f090)
00401816 push offset std::cout (0047ce98)
0040181B call @ILT+660(std::operator<<) (00401299) ; 题外话,观察一下进栈顺序
00401820 add esp,8
00401823 mov ecx,eax
00401825 call @ILT+480(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011e5)
42: HowMany h2 = f(h);
0040182A push ecx
0040182B mov ecx,esp ; 当前 ESP 所指的栈块作为临时对象Temp的内存地址
0040182D mov dword ptr [ebp-18h],esp
00401830 lea eax,[h]
00401833 push eax ; 将h的内存地址[h]压入堆栈
00401834 call @ILT+0(HowMany::HowMany) (00401005) ; 调用拷贝构造函数,把h的内容拷贝到Temp的内存中
00401839 mov dword ptr [ebp-1Ch],eax
0040183C lea ecx,[h2]
0040183F push ecx ; 将h2的内存地址[h2]压入堆栈
00401840 call @ILT+640(f) (00401285) ; 调用f()函数
00401845 add esp,8
00401848 mov dword ptr [ebp-20h],eax
43: }
0040184B lea ecx,[h2]
0040184E call @ILT+500(HowMany::~HowMany) (004011f9) ; 调用析构函数,销毁h2
00401853 mov dword ptr [ebp-4],0FFFFFFFFh
0040185A lea ecx,[h]
0040185D call @ILT+500(HowMany::~HowMany) (004011f9) ; 调用析构函数,销毁h
44: // getchar();
45: return 0;
00401862 xor eax,eax
46: } ///:~
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11