Array Location Arithmetic

[Ch. 1: Overview and History]
[Syntax]
[Names and Scope]
[Types and Type Systems]
[Semantics]
[Functions]
[Memory Management]
[Imperitive Programs and Functional Abstraction]
[Modular and Class Abstraction]
[Functional Programming]
[Logic Programming]

Arrays are typically layed out continguously in memory. The basic parameters are:

α | The starting address of the array. |

e | The size of an array element. |

lb, ub | The lower and upper array bounds. |

a[i] | The subscript expression being evaluated. |

So, for a one-dimensional array,

α

a[i]

a[*lb*..*ub*]

i-*lb* slots

The address of address of the location `a[i]` is given by:

This can be generalized for a two-dimensional array. Note that the first cell of each row is located in memory just after the last cell of the row above it.

α

a[i,j]

a[*lb*1..*ub*1, *lb*2..*ub*2]

i−*lb*1

rows

rows

j−*lb*2 slots

So the address of `a[i]` will be given by starting from α and
first skipping the rows above it, then the cells to its left.

= (α −

Languages which create multi-dimensional arrays as shown here typically require that the bound be constants. That means that the address formulas can be reduced at compile to time to a linear computation of the subscripts (the sum of a constant and a constant multiple of each subscript).

In languages like C and Java, where the lower bound is zero, and the
upper bound is the size less 1, we can substitute 0 for each *lb* and
*s*−1 for each *ub* and get: