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
bandVis a q×r×s array with elements of typet, then the address ofV(i,j,k)will bebase(V) + sizeof(t) * ((i-b)*r*s + (j-b)*s + (k-b)).
Specifically, the address ofV(5,10,20)will be5000 + 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 5 1 Call by Reference 6 2 Call by Copy-Result 5 2 Call by Name 8 2 - 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:
- pointer to a function which takes a double and an array of doubles, returning a double
- 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.