一直以来对传址调用和引用调用都有些分不清楚,虽然给这两者的区别加一大堆区别。
#include "stdafx.h" #include <Windows.h> void funptr(int* ptr) { *ptr=123; } void funcref(int &ref) { ref=234; } __declspec(naked)int _tmain(int argc, _TCHAR* argv[]) { int n; int *ptr; ptr=&n; funptr(ptr); funcref(n); system("pause"); _asm ret; }
代码很简单,就是定义两个函数,一个是以传地址的形式调用,另一个是引用调用。而main函数前面之所以加上__declspec(naked)修饰是为了防止编译器生成多余的汇编代码。而由于加了这个修饰符,导致不能直接在后面加return语句,所以只好调用汇编指令ret返回。
__declspec(naked)int _tmain(int argc, _TCHAR* argv[]) { int n; int *ptr; ptr=&n; 01001420 lea eax,[n] 01001423 mov dword ptr [ptr],eax funptr(ptr); 01001426 mov eax,dword ptr [ptr] 01001429 push eax 0100142A call funptr (10011C7h) 0100142F add esp,4 funcref(n); 01001432 lea eax,[n] 01001435 push eax 01001436 call funcref (1001069h) 0100143B add esp,4 system("pause"); 0100143E mov esi,esp 01001440 push offset string "pause" (100573Ch) 01001445 call dword ptr [__imp__system (10082B0h)] 0100144B add esp,4 0100144E cmp esi,esp 01001450 call @ILT+315(__RTC_CheckEsp) (1001140h) _asm ret; 01001455 ret
通过上面的代码,我们可以看到funcptr和funcref的调用其实是一样的——都是通过EAX传递n的地址值。然后call调用。所以其实传地址调用和传参数调用是一样的。不过你要是在考试的时候遇到这样的问题,你还是参照树上的解释吧,考试和实践有着很大的差别。