MC logo

Array Access Problem Solution

  Array Access Problem

On the dope vector for

a: array[2..10, 3..8] of integer;
First, the long way:

To find the L-value for a[i,j], you must skip down i rows, then along j columns. We move along a row the distance past the beginning, that is, the distance past 3. Each step is of size 4 bytes, so we move along the row a distance of 4×(j−3). To reach the row, we must likewise skip down i rows from the start of the array. The size of a row is 4×(8−3+1), and we need to pass i−2 of them. Therefore, the L-value of the a[i,j] is

5000 + 4×(8−3+1)×(i−2) + 4×(j−3)
After algebraic surgery, that expression becomes:
4940 + 24×i + 4×j
Which produces the vector:
V0 = 4940
d1 = 24
d2 = 4

The short way is to recall that the algebra has already been done. The d2 value is always the size of the array element type, and

d1 = (U2 − L2 + 1)×d2,
V0 = α − L1×d1 - L2×d2.
Then plug.

The vector tells you that the location of a[5, 7], for instance, is

4940 + 5×24 + 7×4 = 5088

For the slice, b = a[*,6], you need to see that b[i] is just another way to write a[i,6]. So the L-value of b[i] can be given as:

4940 + 24×i + 4×6 = 4964 + 24×i
Which can be computed from this dope vector:
V0 = 4964
d1 = 24
The slides give the formula
V0 = L-value(A[L1, j]) - d1×L1 = L-value(A[2, 6]) − 48 = 5012 &minus 48 = 4964
Along with d1 for the step value, which produces the same vector.