先献上源代码
#include
class CNumber
{
public:
CNumber()
{
m_nNumber = 1;
}
int m_nNumber;
};
void main()
{
CNumber *pNumber = NULL;
pNumber = new CNumber;
pNumber->m_nNumber = 3;
printf("%d \r\n" , pNumber->m_nNumber);
}
反汇编
1:
2: class CNumber
3: {
4: public:
5: CNumber()
00401060 push ebp
00401061 mov ebp,esp
00401063 sub esp,44h
00401066 push ebx
00401067 push esi
00401068 push edi
00401069 push ecx
0040106A lea edi,[ebp-44h]
0040106D mov ecx,11h
00401072 mov eax,0CCCCCCCCh
00401077 rep stos dword ptr [edi]
00401079 pop ecx
0040107A mov dword ptr [ebp-4],ecx
6: {
7: m_nNumber = 1;
0040107D mov eax,dword ptr [ebp-4]
00401080 mov dword ptr [eax],1
8: }
00401086 mov eax,dword ptr [ebp-4]
00401089 pop edi
0040108A pop esi
0040108B pop ebx
0040108C mov esp,ebp
0040108E pop ebp
0040108F ret
9: int m_nNumber;
10: };
11:
12: void main()
13: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,44h
00401026 push ebx
00401027 push esi
00401028 push edi
00401029 lea edi,[ebp-44h]
0040102C mov ecx,11h
00401031 mov eax,0CCCCCCCCh
00401036 rep stos dword ptr [edi]
14: CNumber number;
00401038 lea ecx,[ebp-4] //ecx对象首地址
0040103B call @ILT+0(CNumber::CNumber) (00401005)
15: }
00401040 pop edi
00401041 pop esi
00401042 pop ebx
00401043 add esp,44h
00401046 cmp ebp,esp
00401048 call __chkesp (004010a0)
0040104D mov esp,ebp
0040104F pop ebp
00401050 ret
修改后:
12: void main()
13: {
00401020 push ebp
00401021 mov ebp,esp
00401023 push 0FFh
00401025 push offset __ehhandler$_main (004132fb)
0040102A mov eax,fs:[00000000]
00401030 push eax
00401031 mov dword ptr fs:[0],esp
00401038 sub esp,50h
0040103B push ebx
0040103C push esi
0040103D push edi
0040103E lea edi,[ebp-5Ch]
00401041 mov ecx,14h
00401046 mov eax,0CCCCCCCCh
0040104B rep stos dword ptr [edi]
14: CNumber *pNumber = NULL;
0040104D mov dword ptr [ebp-10h],0
15: pNumber = new CNumber;
00401054 push 4
00401056 call operator new (00401250)
0040105B add esp,4
0040105E mov dword ptr [ebp-18h],eax //返回值 内存首地址
00401061 mov dword ptr [ebp-4],0 //保存申请次数
00401068 cmp dword ptr [ebp-18h],0 //是否为NULL
0040106C je main+5Bh (0040107b)
0040106E mov ecx,dword ptr [ebp-18h] //ecx this指针
00401071 call @ILT+0(CNumber::CNumber) (00401005)
00401076 mov dword ptr [ebp-1Ch],eax
00401079 jmp main+62h (00401082)
0040107B mov dword ptr [ebp-1Ch],0
00401082 mov eax,dword ptr [ebp-1Ch]
00401085 mov dword ptr [ebp-14h],eax
00401088 mov dword ptr [ebp-4],0FFFFFFFFh
0040108F mov ecx,dword ptr [ebp-14h]
00401092 mov dword ptr [ebp-10h],ecx
16: pNumber->m_nNumber = 3;
00401095 mov edx,dword ptr [ebp-10h]
00401098 mov dword ptr [edx],3
17: printf("%d \r\n" , pNumber->m_nNumber);
0040109E mov eax,dword ptr [ebp-10h]
004010A1 mov ecx,dword ptr [eax]
004010A3 push ecx
004010A4 push offset string "%d \r\n" (0042501c)
004010A9 call printf (004011d0)
004010AE add esp,8
18: }
004010B1 mov ecx,dword ptr [ebp-0Ch]
004010B4 mov dword ptr fs:[0],ecx
004010BB pop edi
004010BC pop esi
004010BD pop ebx
004010BE add esp,5Ch
004010C1 cmp ebp,esp
004010C3 call __chkesp (00401650)
004010C8 mov esp,ebp
004010CA pop ebp
004010CB ret
#include
#include
class CMyString
{
public:
CMyString()
{
m_pString = NULL;
}
CMyString(CMyString & str)
{
int nLen = strlen(str.m_pString);
this->m_pString = new char[nLen + sizeof(char)];
strcpy(this->m_pString , str.m_pString);
}
~CMyString()
{
if(m_pString != NULL)
{
delete [] m_pString;
m_pString = NULL;
}
}
void SetString(char * pStr)
{
int nLen = strlen(pStr);
if(m_pString != NULL)
{
delete [] m_pString;
m_pString = NULL;
}
m_pString = new char[nLen + sizeof(char)];
strcpy(m_pString , pStr);
}
char * m_pString;
};
void show(CMyString str)
{
printf(str.m_pString);
}
CMyString GetMyString()
{
CMyString str;
str.SetString("HELLO");
return str;
}
CMyString* GetMyString(CMyString *pMyString)
{
CMyString str;
str.SetString("HELLO");
pMyString = &str;
return &str;
}
CMyString g_cms1;
CMyString g_cms2;
void main()
{
// CMyString myString;
// myString.SetString("Hello");
// show(myString);
// show(GetMyString());
CMyString *pStr = new CMyString[3];
}
1: #include
2: #include
3: class CMyString
4: {
5: public:
6: CMyString()
004012F0 push ebp
004012F1 mov ebp,esp
004012F3 sub esp,44h
004012F6 push ebx
004012F7 push esi
004012F8 push edi
004012F9 push ecx
004012FA lea edi,[ebp-44h]
004012FD mov ecx,11h
00401302 mov eax,0CCCCCCCCh
00401307 rep stos dword ptr [edi]
00401309 pop ecx
0040130A mov dword ptr [ebp-4],ecx
7: {
8: m_pString = NULL;
0040130D mov eax,dword ptr [ebp-4]
00401310 mov dword ptr [eax],0
9: }
00401316 mov eax,dword ptr [ebp-4]
00401319 pop edi
0040131A pop esi
0040131B pop ebx
0040131C mov esp,ebp
0040131E pop ebp
0040131F ret
10: CM