Calculate Memory Addresses
in Arrays
Jayanta Pal
Dept. of IT
Tripura University
Single-dimensional Array
• In 1-D array, there is no Row Major and no Column major concept, all
the elements are stored in contiguous memory locations. We can
calculate the address of the 1-D array by using the following formula:
• Address of A[I] = Base Address + W * I.
• Where I[ is a location(Indexing) of element] whose address is to be
found out. W (is the size of data type).
Single-dimensional Array
• Address of A[I] = Base Address + W * I.
Two Dimensional Array
• The 2-dimensional arrays are stored as
1-dimensional arrays in the computer’s
memory.
• There are two ways to achieve this:
• Row-major Implementation
• Column-major Implementation
Row-major Implementation
• In this method, the first row elements are placed first, then the second row
elements and so on.
• The formula to calculate the address of [I, J]th block is:
• Address of [I, J]th element in row-major = B + W*[C*(I – Lr) + (J – Lc)]
= B + W * ( C * I + J)
B is the base address (address of the first block in the array).
W is the width in bytes (size in bytes for each block in the array).
Lr is the index of the first row. (normally starts with 0)
Lc is the index of the first column. (normally starts with 0)
R is the total number of rows.
C is the total number of columns.
Row-major order
Address of A[I][J] = Base Address + W * ( C * I + J)
Column-major Implementation
• In this method, the first column elements are placed first, then the second
column elements and so on.
• The formula to calculate the address of [I, J]th block is:
• Address of [I, J]th element in column-major = B + W*[R*(J – Lc) + (I – Lr)]
= B + W * ( R * J + I)
B is the base address (address of the first block in the array).
W is the width in bytes (size in bytes for each block in the array).
Lr is the index of the first row. (normally starts with 0)
Lc is the index of the first column. (normally starts with 0)
R is the total number of rows.
C is the total number of columns.
Column-major order
Address of A[I][J] = Base Address + W * ( R * J + I)
The formula for computing the address using column-
major ordering is to simply reverse the indexes and sizes
in the computation.
Questions based on Array Implementation
• A matrix B[10][20] is stored in the memory with each element
requiring 2 bytes of storage. If the base address at B[2][1] is 2140,
find the address of B[5][4] when the matrix is stored in Column Major
Wise.
• Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
• = 2140 + 2[10(4 – 1) + (5 – 2)]
• = 2140 + 2[10 × 3 + 3]
= 2140 + 2[30 + 3]
= 2140 + 2[33]
= 2140 + 66
= 2206
• Each element of an array arr[15][20] requires ‘W’ bytes of storage. If the
address of arr[6][8] is 4440 and the base address at arr[1][1] is 4000, find the
width ‘W’ of each cell in the array arr[][] when the array is stored as Column
Major Wise.
• Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
• ⇒ 4440 = 4000 + W[15(8 – 1) + (6 – 1)]
• ⇒ 4440 = 4000 + W[15(7) + 5]
⇒ 4440 = 4000 + W[105 + 5]
⇒ 4440 = 4000 + W[110]
⇒ W[110] = 440
⇒ W = 4.
• A matrix ARR[-4…6, 3…8] is stored in the memory with each element requiring
4 bytes of storage. If the base address is 1430, find the address of ARR[3][6]
when the matrix is stored in Row Major Wise.
• Number of columns, C = 8 – 3 + 1 = 6.
• Number of rows, R = 6 – (-4) + 1 = 11.
• Dimension of the array: Rows = -4 to 6 = 11 and Columns= 3 to 8 = 6
• So it is ARR[11][6]
• Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
• ⇒ Address of ARR[3][6] = 1430 + 4[6(3 – (-4)) + (6 – 3)]
• ⇒ Address of ARR[3][6] = 1430 + 4[6(3 + 4) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[6(7) + 3]
⇒ Address of ARR[3][6] = 1430 + 4[42 + 3]
⇒ Address of ARR[3][6] = 1430 + 4[45]
⇒ Address of ARR[3][6] = 1430 + 180
⇒ Address of ARR[3][6] = 1610.
• A matrix A[m][m] is stored in the memory with each element
requiring 4 bytes of storage. If the base address at A[1][1] is 1500 and
the address of A[4][5] is 1608, determine the order of the matrix
when it is stored in Column Major Wise.
• Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
• ⇒ 1608 = 1500 + 4[m(5 – 1) + (4 – 1)]
• ⇒ 1608 = 1500 + 4[m(4) + 3]
⇒ 1608 = 1500 + 16m + 12
⇒ 1608 = 1512 + 16m
⇒ 16m = 96
⇒ m = 6.
• A matrix A[m][n] is stored with each element requiring 4 bytes of
storage. If the base address at A[1][1] is 1500 and the address at
A[4][5] is 1608, determine the number of rows of the matrix when
the matrix is stored in Column Major Wise.
• Address of [I, J]th element in column-major = B + W[R(J – Lc) + (I – Lr)]
• ⇒ 1608 = 1500 + 4[R(5 – 1) + (4 – 1)]
• ⇒ 1608 = 1500 + 4[4R + 3]
⇒ 1608 = 1500 + 16R + 12
⇒ 1608 = 1512 + 16R
⇒ 16R = 96
⇒ R = 6.
• A matrix P[15][10] is stored with each element requiring 8 bytes of
storage. If the base address at P[0][0] is 1400, determine the address at
P[10][7] when the matrix is stored in Row Major Wise.
• Address of [I, J]th element in row-major = B + W[C(I – Lr) + (J – Lc)]
• ⇒ Address at P[10][7] = 1400 + 8[10(10 – 0) + (7 – 0)]
• ⇒ Address at P[10][7] = 1400 + 8[10(10) + 7]
⇒ Address at P[10][7] = 1400 + 8[100 + 7]
⇒ Address at P[10][7] = 1400 + 8[107]
⇒ Address at P[10][7] = 1400 + 856
⇒ Address at P[10][7] = 2256.