Since some of you seem still to have difficulties with this kind of question, here is a solution. The difference between call-by-value (CBV) and call-by-reference (CBR) is in the isolation between the formal and actual parameters in one case and the identification of these in the other. Now, in most situations CBR produces similar results to call-by-value-result (CBVR) and call-by-name (CBN), so if one part of our example remains within these situations then it would also serve to differentiate CBV from all the others. A swap like program fragment would do very well for this.
CBVR differs from CBR only in the presence of aliasing and we can extend our swap example suitably to bring this out. If this part does not differentiate between lazy and eager address calculation then its behavior under CBN would be the same as that under CBR and this would therefore also distinguish between CBN and CBVR. Finally, distinguishing between CBR and CBN involves exploiting differences between lazy and eager address calculations. Putting all these things together, we might write a code fragment such as the following:
int x;
void swap(int i, int j, int k)
{ int temp;
temp = i;
i = j;
j = temp;
x = 5;
k = 3;
}
int main()
{ int y;
int A[2];
x = 0; y = 1; A[0] = 2; A[1] = 2;
swap(x,y,A[y]);
}
C syntax has been used here but, of course, C does not have all the
different parameter passing mechanisms to match the different
interpretations we want of this code. Make sure to check the results
with the different mechanisms with this example and also to understand
how it encapsulates the thought process I describe above that
leads to this solution.
If you have questions on your individual program, please see Xiaochu. She would be happy to explain the problems with your solution or, if you can convince her that she did not understand your solution properly, to correct your grade. With a problem like this, where everyone's answer is different, it is possible that your solution may not have been understood as completely as it should have been.
The scoring for this problem was broken up as follows: 2 points for depicting or describing stack snapshots in a comprehensive way, 2 points for explaining the control and access link maintenance and 2 points for explaining the compiler support for and the runtime activity involved in resolving variable reference.
Last updated on April 18, 2006 by gopalan@cs.umn.edu and xqi@cs.umn.edu