Homework 4 Solution

Problem 1
Arrays in most languages (excluding Fortran, Matlab, and a few others) are laid out with their last dimension stored contiguously (a n-dimensional generalization of row major order). If indexing starts from b and V is a q×r×s array with elements of type t, then the address of V(i,j,k) will be base(V) + sizeof(t) * ((i-b)*r*s + (j-b)*s + (k-b)).

Specifically, the address of V(5,10,20) will be 5000 + 4 * (4*20*50 + 9 * 50 + 19) = 22876
Scott 8.6
def f(x, y, z):
    x = 2
    return x + y + z
    
a = 1
z = f(a, a, 2*a)
Semantics Value of z Value of a
Call by Value 51
Call by Reference 62
Call by Copy-Result 52
Call by Name 82
Scott 8.13
The representation of an ordinary record places each field at an aligned offset from the start of the record. This means that dereferencing any individual field will not cause an alignment fault. Packed records, on the other hand, store their fields at arbitrary offsets.
Passing the address of a packed record's field as a by-reference parameter might not be valid (since it would cause your program to crash).
Similarly, an element of a smaller range may be represented with fewer bytes than an element of a larger range. A function accepting a too-small range value as a by-reference parameter may write past the end of that range value's storage and corrupt other memory.
Scott 8.14
double ( * foo ( double (*) (double, double[]), double )) (double, ...)

foo is a function which takes as arguments:
  1. pointer to a function which takes a double and an array of doubles, returning a double
  2. a double
and returns a pointer to a function whuch takes a double followed by a variable number of arguments and returns a double.
Scott 8.15
Only if the user-supplied argument values change the runtime behavior of the function. The actual binding of parameters will, however, take the same amount of time. In either case an activation record with the same number of arguments will have to be constructed.