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,
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.
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: