mor*_*ort 14 rpc terminology parameter-passing
调用引用和复制/恢复之间的结果有何不同?
背景:我目前正在研究分布式系统.关于远程过程调用的参考参数的传递,该书指出:"通过引用的调用已被复制/恢复替换.虽然这并不总是相同,但它已经足够了".我理解原则上如何通过引用和复制/恢复工作,但我没有看到结果的差异在哪里?
N_A*_*N_A 18
从这里取的例子.
主要代码:
#include <stdio.h>
int a;
int main() {
a = 3;
f( 4, &a );
printf("%d\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按价值呼叫:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but now nothing is done with x anymore
a += 2*y;
// a is still 3 so the result is 11
}
Run Code Online (Sandbox Code Playgroud)
传入值并且对传入的变量的值没有影响.
通过参考呼叫:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but because & is used x is the same as a
// meaning if you change x it will change a
a += 2*y;
// a is now 6 so the result is 14
}
Run Code Online (Sandbox Code Playgroud)
传入参考.有效地,函数中的变量与外部变量相同.
使用复制/恢复呼叫:
int a;
void unsafe(int x) {
x= 2; //a is still 1
a= 0; //a is now 0
}//function ends so the value of x is now stored in a -> value of a is now 2
int main() {
a= 1;
unsafe(a); //when this ends the value of a will be 2
printf("%d\n", a); //prints 2
}
Run Code Online (Sandbox Code Playgroud)
传入值并且对函数结尾处传递的变量值没有影响,此时函数变量的FINAL值存储在传入的变量中.
然后,通过引用调用和复制/恢复之间的基本区别是,对函数变量所做的更改将不会显示在传入的变量中,直到函数结束之后,将立即看到按引用进行的调用更改.
小智 8
通过复制/恢复呼叫是一种特殊的逐个呼叫的情况,其中提供的参考对于呼叫者是唯一的.在函数结束之前,不会保存引用值的最终结果.
当通过引用调用RPC中的方法时,这种类型的调用很有用.实际数据将发送到服务器端,最终结果将发送到客户端.这将减少流量,因为服务器不会每次更新引用.