What's the difference between call by reference and copy/restore
Asked Answered
S

3

14

What's the difference in the outcome between call by reference and copy/restore?

Background: I'm currently studying distributed systems. Concerning the passing of reference parameters for remote procedure calls, the book states that: "the call by reference has been replaced by copy/restore. Although this is not always identical, it is good enough". I understand how call by reference and copy/restore work in principle, but I fail to see where a difference in the result may be?

Shadbush answered 13/1, 2012 at 9:33 Comment(2)
Although I was able to accept your answer, I had to wait for a few more hours to be able to award the bounty. Thanks for your great answer and the bounty is yours of course!Shadbush
Ah, I didn't know that about the bounty system. Good to know!Lightface
L
21

Examples taken from here.

Main code:

#include <stdio.h>

  int a;

  int main() {
      a = 3;
      f( 4, &a );
      printf("&#37;d\n", a);
      return 0;
  }

Call by Value:

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
}

Value is passed in and has no effect on the value of the variable passed in.

Call by Reference:

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
}

Reference is passed in. Effectively the variable in the function is the same as the one outside.

Call with Copy/Restore:

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
}

Value is passed in and has no effect on the value of the variable passed in UNTIL the end of the function, at which point the FINAL value of the function variable is stored in the passed in variable.

The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.

Lightface answered 15/1, 2012 at 16:36 Comment(6)
you used &y in both of themExpressway
In the example "Call with Copy/Restore", it prints '0' not '2'.Restraint
@Restraint which language which supports copy/restore did you use?Lightface
@Restraint no problem :-)Lightface
I really don't get how this answer got accepted and 14 upvotes. Something must be wrong with me. Both call by value and reference code are same only comments differ. In both x can never be 6 but 7 as 4 is passed to f(). Tell me if I am wrong.Frow
@Frow Do you mean the syntax doesn't change? This is all pseudo-code which looks roughly like C. C doesn't support copy/restore at all, so don't get too caught up on the syntax. Assume in each case that the stated call parameter behavior holds true.Lightface
W
9

Call by Copy/Restore is a special case of call-by-reference where the provided reference is unique to the caller. The final result on the referenced values will not be saved until the end of the function.

This type of calling is useful when a method in RPC called by reference. The actual data is sent to the server side and the final result will send to the client. This will reduce the traffic, since the server will not update the reference each time.

Wall answered 28/4, 2013 at 12:18 Comment(0)
P
1

Call By Reference:
In call-by-reference, we pass a pointer to the called function. Any changes that happens to the data pointed by that pointer will be reflected immediately.
Suppose if there are numerous changes to be made to that data, while it wouldn’t incur much cost locally, it’ll be expensive in terms of network cost as for each change data will have to be copied back to the client.
C Code:

 void addTwo(int *arr, int n){
  for(int i=0;i<n;i++){
    arr[i]+=2;  //change is happening in the original data as well
  }
}
 int main(){
  int arr[100]={1,2,3,...}; // assuming it to be initialised
  addTwo(arr,100);  
}

Call By Copy/Restore:
In call-by-copy/restore, the idea is that when the function is called with the reference to the data, only the final result of the changes made to the data is copied back to the original data(when the function is about to return) without making any changes to the original data during the function call, requiring only one transfer back to the client.
In the C code below, the data pointed by arr is copied in the function and stored back to arr after all the changes to the local data are finalised.
C Code:

 void addTwo(int *arr, int n){
  // copy data locally
  larr = (int*)malloc(n*sizeof(int));
  for(int i=0;i<n;i++){
    larr[i]=arr[i]; 
  }
  for(int i=0;i<n;i++){
    // change is happening to the local variable larr
    larr[i]+=2;  
  }
  //copy all the changes made to the local variable back to the original data
  for(int i=0;i<n;i++){
    arr[i]=larr[i]; 
  }
}

 int main(){
  int arr[100]={1,2,3,...}; // assuming it to be initialised
  addTwo(arr,100);  
}

Note: Code shown above doesn’t represent actual RPC implementation, just an illustration of the concepts. In real RPC, complete data is passed in the message instead of pointers(addresses).

Postmeridian answered 6/5, 2021 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.