<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><!--Converted with LaTeX2HTML 96.1-beta (Jan 15, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds -->CS471 Programming Language Structure I
Spring 2000

Homework 2

  1. For the following program fragment (you can assume C syntax and static scoping), draw diagrams showing each stage in the life of the run-time stack from the start of execution until routine beta is called recursively by alpha. Show how the dynamic and static links are set at each stage, as well as values for all the variables.

int i = 1, j = 2, k = 3;
beta();

alpha() {
  int i = 4, l = 5;
  ...
  i += k + l;
  beta();
  ...
}

beta() {
  int k = 6;
  ...
  i = j + k;
  alpha();
  ...
}

main() {
  ...
  beta();
  ...
}

 

  1. Consider the program below. Discuss call by reference and call by value-result for the call swap(a[i], a[j]), where a is an array of integers. What happens if i = j?

 

swap(int x, int y) {
  x = x + y;
  y = x - y;
  x = x - y;
}

Assignment due: Friday, March 24th., in class.