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
注意到 不論是傳址或傳參照,翻出來的程式碼都是一樣的。
換句話說實際上它們都是以傳址來實現(沒有所謂傳照就可以不複製地址)
http://ehome.hifly.to/showthread.php?s=&threadid=148

2.三者的用法

//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 語言 常見題目 | 易春木

發表迴響

Copy Protected by Chetan's WP-Copyprotect.