Implementation of arrays
Implementation of one dimensional array in memory
The address of a particular element in a one-dimensional array is given by the relation:
Address of element a[k] = {Base address} + {Size of each element in array} * {Index of the array}
Implementation of two dimensional arrays in memory
A two dimensional array can be implemented in two ways:
1. Row-major implementation.
2. Column-major implementation.
Row-major implementation is a linear technique in which elements of the array are allocated in memory in a row-wise manner i.e., first row is stored firstly, then the second row is stored and likewise. For example, an array a[3][3] is stored in the memory a shown:
Address of element a[i][j] = B + S (p (i - L1) + (j – L2))
Where B is the base address of the array, S is the size of each array element, p is the number of columns, L1 is the lower bound of the row and L1 is the lower bound of the column.
Similarly for column major implementation allocation is done column by column i.e., elements of the first column are stored firstly, then the second column elements and so on. For example, an array a[3][3] is stored in the memory a shown:
Address of element a[i][j] = B + S (q (j – L2) + (i – L1))
Where B is the base address of the array, S is the size of each array element, q is the number of columns, L1 is the lower bound of the row and L1 is the lower bound of the column.
Posted in Computer Science, Information Technology, Data Structure, Data Structure |
