I was looking at call by reference and call by copy/restore. And I had some confusion. Suppose I have a 2-parameter function that increments 1 to each parameter. Now what will happen if I use both the parameters as the same variable, like incr(i, i)
, in the case of call by reference and call by copy/restore?
Asked
Active
Viewed 3,190 times
2

Linda Nu
- 57
- 1
- 8
-
1C only has 'pass by value'. Therefore, you'd have to simulate 'pass by reference' by passing a pointer, and I'm not sure what semantics you expect from copy/restore; too much depends on who does the copying and who does the restoring. Maybe Wikipedia's [Evaluation Strategy](http://en.wikipedia.org/wiki/Evaluation_strategy) article can help; it has a section on 'call by copy/restore'. – Jonathan Leffler Feb 07 '15 at 05:52
-
You might also find [What's the difference between call by reference and copy/restore?](http://stackoverflow.com/questions/8848402/whats-the-difference-between-call-by-reference-and-copy-restore) relevant. Is there a reason question this should not be closed as a duplicate of that one? – Jonathan Leffler Feb 07 '15 at 05:57
-
Yes, I specifically wanted the implementation of both cases with respect to the incr(i,i) function. – Linda Nu Feb 07 '15 at 22:31
1 Answers
3
The question What's the difference between call by reference and copy/restore covers this in part, but it isn't quite complete. The Wikipedia article on Evaluation Strategy is helpful.
Here are two implementations and calls for a function incr()
that takes two arguments and increments each one.
Call by reference
#include <stdio.h>
void incr(int *pi1, int *pi2)
{
(*pi1)++;
(*pi2)++;
}
int main(void)
{
int i = 57;
printf("%d\n", i);
incr(&i, &i);
printf("%d\n", i);
return 0;
}
Output:
57
59
Call by copy/restore
The incr
function is actually unchanged from the call by reference version.
#include <stdio.h>
void incr(int *pi1, int *pi2)
{
(*pi1)++;
(*pi2)++;
}
int main(void)
{
int i = 57;
printf("%d\n", i);
{
int cr_0000 = i; // Copy for first argument
int cr_0001 = i; // Copy for second argument
incr(&cr_0000, &cr_0001);
i = cr_0001; // Restore for second argument
i = cr_0000; // Restore for first argument
}
printf("%d\n", i);
return 0;
}
Output:
57
58
Note that with this incr()
function, it does not matter which order the values are restored, but if the incr()
function treated its arguments asymmetrically (say, added 1 to the first and 2 to the second), then the final result would depend on whether cr_0000
is restored before cr_0001
or vice versa.

Community
- 1
- 1

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278
-
1Though I get how the output is generated...I don't get the whole point behind copy/restore. Its "make copy of original variable, pass the copies by reference, reassign copies back to the original variable". What's great to give a dedicated name "copy/restore"? – Mahesha999 Dec 21 '16 at 15:52
-
@Mahesha999 The second one is also call by reference, he's just done something different with it. I hadn't heard it whatsoever until I saw it in some exam paper and I think they made it up – Lewis Kelsey May 12 '21 at 20:44