64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
void test(int *a)
{
a=new int;
*a=4;
}
void testRefer(int *&a)
{
a=new int;
*a=4;
}
int main()
{
int *b;
test(b);
testRefer(b);
cout<<*b<<endl;
return 0;
}
我查了一下汇编代码:
void testRefer(int *&a)
{
01081420 push ebp
01081421 mov ebp,esp
01081423 sub esp,0CCh
01081429 push ebx
0108142A push esi
0108142B push edi
0108142C lea edi,[ebp-0CCh]
01081432 mov ecx,33h
01081437 mov eax,0CCCCCCCCh
0108143C rep stos dword ptr es:[edi]
a=new int;
0108143E push 4
01081440 call operator new (1081186h)
01081445 add esp,4
01081448 mov dword ptr [ebp-0C8h],eax
0108144E mov eax,dword ptr [a]
01081451 mov ecx,dword ptr [ebp-0C8h]
01081457 mov dword ptr [eax],ecx
*a=4;
01081459 mov eax,dword ptr [a]
0108145C mov ecx,dword ptr [eax]
0108145E mov dword ptr [ecx],4
}
01081464 pop edi
01081465 pop esi
01081466 pop ebx
01081467 add esp,0CCh
0108146D cmp ebp,esp
0108146F call @ILT+310(__RTC_CheckEsp) (108113Bh)
01081474 mov esp,ebp
01081476 pop ebp
01081477 ret
void test(int *a)
{
010813B0 push ebp
010813B1 mov ebp,esp
010813B3 sub esp,0CCh
010813B9 push ebx
010813BA push esi
010813BB push edi
010813BC lea edi,[ebp-0CCh]
010813C2 mov ecx,33h
010813C7 mov eax,0CCCCCCCCh
010813CC rep stos dword ptr es:[edi]
a=new int;
010813CE push 4
010813D0 call operator new (1081186h)
010813D5 add esp,4
010813D8 mov dword ptr [ebp-0C8h],eax
010813DE mov eax,dword ptr [ebp-0C8h]
010813E4 mov dword ptr [a],eax
*a=4;
010813E7 mov eax,dword ptr [a]
010813EA mov dword ptr [eax],4
}
010813F0 pop edi
010813F1 pop esi
010813F2 pop ebx
010813F3 add esp,0CCh
010813F9 cmp ebp,esp
010813FB call @ILT+310(__RTC_CheckEsp) (108113Bh)
01081400 mov esp,ebp
01081402 pop ebp
01081403 ret
test(b);
010835C5 cmp byte ptr [ebp-0D1h],0
010835CC jne main+3Bh (10835DBh)
010835CE push offset (1083680h)
010835D3 call @ILT+165(__RTC_UninitUse) (10810AAh)
010835D8 add esp,4
010835DB mov eax,dword ptr [b]
010835DE push eax
010835DF call test (1081140h)
010835E4 add esp,4
testRefer(b);
010835E7 mov byte ptr [ebp-0D1h],1
010835EE lea eax,[b]
010835F1 push eax
010835F2 call testRefer (10811D6h)
010835F7 add esp,4
原来函数汇编出来的代码完全一样,只是调用时候不一样。
能不能解释一下调用时候他们的区别?
就是下面两段代码的区别:
test(b);
010835C5 cmp byte ptr [ebp-0D1h],0
010835CC jne main+3Bh (10835DBh)
010835CE push offset (1083680h)
010835D3 call @ILT+165(__RTC_UninitUse) (10810AAh)
010835D8 add esp,4
010835DB mov eax,dword ptr [b]
010835DE push eax
010835DF call test (1081140h)
010835E4 add esp,4
testRefer(b);
010835E7 mov byte ptr [ebp-0D1h],1
010835EE lea eax,[b]
010835F1 push eax
010835F2 call testRefer (10811D6h)
010835F7 add esp,4
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void test(vector<string> & vec) {
for (int i=0;i<3;i++) {
vec.push_back("ajfkd");
}
}
void show(vector<string> & vec) {
for (int i=0;i<3;i++) {
cout << vec[i] << endl;
}
}
int main()
{
vector<string> vec;
test(vec);
show(vec);
return 0;
}