2-dimensional array
. storage can be either row major or column major
. in case of 2-D array stored in row major form address of A[i1 , i2 ] can be calculated as
base + ((i1 - low 1 ) x n2 + i2 - low 2 ) x w
where n 2 = high2 - low2 + 1
. rewriting the expression gives
((i1 x n 2 ) + i 2 ) x w + (base - ((low 1 x n2 ) + low 2 ) x w)
((i1 x n2 ) + i2 ) x w + constant
. this can be generalized for A[i1 , i2 ,., ik ]
Similarly, for a row major two dimensional array the address of A[i][j] can be calculated by the formula :
base + ((i-lowi )*n2 +j - lowj )*w where low i and lowj are lower values of I and j and n2 is number of values j can take i.e. n2 = high2 - low2 + 1.
This can again be written as :
((i * n2) + j) *w + (base - ((lowi *n2) + lowj ) * w) and the second term can be calculated at compile time.
In the same manner, the expression for the location of an element in column major two-dimensional array can be obtained. This addressing can be generalized to multidimensional arrays.
|