Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

C/C++: 請說明 call by value, address, reference 三者差異? (比較速度/用法/詳細補充)

1. 速度效能

Reference (address) > Value
call by value 是速度最慢的,call by ref與 call by addr 速度相同! call by value慢是因為它必須先 copy一份再傳給被呼叫者

64: Call_by_ref(k1);//call by reference
004012F8 lea eax,[ebp-4] ;抓出k1的指標
004012FB push eax ;從stack傳出指標參數
004012FC call @ILT+10(Call_by_ref) (0040100f) ;呼叫Call_by_ref
00401301 add esp,4 ;平衡stack
65: Call_by_ptr(&k1);//call by address
00401304 lea ecx,[ebp-4] ;抓出k1的指標
00401307 push ecx ;從stack傳出指標參數
00401308 call @ILT+15(Call_by_ptr) (00401014);呼叫Call_by_ptr
0040130D add esp,4 ;平衡stack
注意到 不論是傳址或傳參照,翻出來的程式碼都是一樣的。


//call by value

//call by address

//call by reference


Call by Value


Call by Pointer


Call by Reference

3 thoughts on “C/C++: 請說明 call by value, address, reference 三者差異? (比較速度/用法/詳細補充)

  1. Pingback: 軟體面試 C 語言 常見題目 | 易春木