Memory Address Calculation of any Element in Array (1D, 2D, 3D)

 In this class we will see how to calculate the address of an element in 1-dimensional, 2-dimensional, and 3-dimensional arrays, using both row-major and column-major order

1-D array: Address Calculation of Any Element  

A one-dimensional array, also known as a single-dimensional array, is a linear array where elements are accessed using a single index. This index represents either a row or a column position in the array.

Formula:

To calculate the address of a specific element in an array, use this formula:

Address of A[Index] = Base + Size * (Index – LB)

Where:

  • A : Array Name
  • Index: The position of the element whose address is being calculated (not the element’s actual value).
  • Base: The base address of the array (starting address in memory).
  • Size: The storage size of each element in bytes.
  • LB: The lower bound value (default is 0 if not specified).

Question:

Given an array A[500 ………… 800], with:

  • Base address (Base) = 2000
  • Size of each element (Size) = 4 bytes
  • Lower bound (LB) = 500

Find the address of A[750]?

Note: A[500 ………… 800] means, array containing elements from position 500 to 800.

Formula and Calculation:

Address of A[Index] = Base + Size * (Index – LB)
Address of A[750] = 2000 + 4 * (750 – 500)
                              = 2000 + 4 * (250)
                              = 2000 + 1000
Address of A[750] = 3000

2-D array: Address Calculation of Any Element  

The 2-dimensional array also called array of arrays. Imagine a 2D array as a grid, similar to a matrix,

where each element has a specific row and column position.

To find the address of an element in a 2D array, there are two main methods:

  • Row Major Order: Elements are stored row-by-row.
  • Column Major Order: Elements are stored column-by-column

2-D array:  Row Major Order

In a row-major order, the elements of an array are stored one row at a time. This means all elements in the first row are stored first in memory, then all elements in the second row, and so on.

To find the address of an element in a 2D array using row-major order, we use this formula:

Address of A[row][col] = Base + Size * ((row – SR) * total_cols + (col – SC))

Here’s what each part means:

  • row: Row number of the element we want to find.
  • col: Column number of the element we want to find.
  • Base: The starting address of the array in memory.
  • Size: The size (in bytes) of each element in the array.
  • SR: The starting row index (assume 0 if not given).
  • SC: The starting column index (assume 0 if not given).
  • total_cols: Total number of columns in the array.

Question 

Given an array, arr[2………12][3………18] with a base address of 200 and each element having a size of 2 bytes in memory, find the address of arr[10][5] using row-major order.

Note: arr[2…12][3…18], means rows ranging from 2 to 12 and columns from 3 to 18.

Solution:

  • Base = 200
  • Size = 2 bytes
  • row = 10
  • col = 5
  • SR = 2
  • SC = 3
  • total_cols= rang [3 to 18] =16

Formula:

Address of A[row][col] = Base + Size * {(row – SR) * total_cols + (col – SC)}

Solution:

Address of A[10][5] = 200 + 2 * {(10 – 2) * 16 + (5 – 3)}
= 200 + 2 * {(8) * 16 + (2)}
= 100 + 2 * {130}
Address of A[10][5] = 460

 

2D array: Column Major Order

In a column-major order, the elements of an array are stored one column at a time. This means all elements in the first column are stored first in memory, then all elements in the second column, and so on.

To find the address of an element in a 2D array using column-major order, we use this formula:

Address of A[row][col] = Base + Size * {(col – SC)* total_rows + (row – SR)}

Here’s what each part means:

  • row: Row number of the element we want to find.
  • col: Column number of the element we want to find.
  • Base: The starting address of the array in memory.
  • Size: The size (in bytes) of each element in the array.
  • SR: The starting row index (assume 0 if not given).
  • SC: The starting column index (assume 0 if not given).
  • total_rows: Total number of rows in the array.

Example:

Given an array, arr[2………12][3………18] with a base address of 200 and each element having a size of 2 bytes in memory, find the address of arr[10][5] using column-major order.

Note: arr[2…12][3…18] means rows ranging from 2 to 12 and columns from 3 to 18.

Given:

  • Base = 200
  • Size = 2 bytes
  • row = 10
  • col = 5
  • SR = 2
  • SC = 3
  • total_rows = range [2 to 12] = 11

Formula:

Address of A[row][col] = Base + Size * {(col – SC)* total_rows + (row – SR)}

Solution

Address of A[10][5] = 200 + 2 * {(5 – 3) * 11 + (10 – 2)}
= 200 + 2 * {(2) * 11 + (8)}
= 200 + 2 * {30}
Address of A[10][5] = 260

The address of A[10][5] in column-major order is 260.

 

3-D array: Address Calculation of Any Element  

3-Dimensional array is a collection of 2-Dimensional arrays. It is specified by using three subscripts:

  • Block size
  • Row size
  • Column size

More dimensions in an array mean more data can be stored in that array. 

To find the address of any element in 3-Dimensional arrays there are the following two ways-

  • Row Major Order
  • Column Major Order

 

3D Array: Row Major Order

In row-major order, the elements of a 3D array are stored one row at a time within each block. Once all rows in a block are stored, it moves to the next block.

The formula for finding the address of an element in a 3D array in row-major order is:

Address of A[block][row][col] = Base + Size* {(total_rows * total_cols * (Block-SB) + +(row−SR) * total_cols+(col−SC)}

Where:

  • Base = Starting address of the array
  • Size = Size (in bytes) of each element in the array
  • Block, row, col = Position of the element within the block, row, and column, respectively
  • SB, SR, SC = Starting (lower) indices of the blocks, rows, and columns
  • total_rows = Total number of rows
  • total_cols = Total number of columns

Question:

Given a 3D array arr[3:7, -2:2, 0:4] with:

  • Base address Base = 500
  • Size of each element Size = 3 bytes
  • Find the address of arr[6][0][3] using row-major order.

Solution:

Given:

  • Base = 500
  • Size = 3 bytes
  • Block = 6
  • row = 0
  • col = 3
  • SB = 3, SR = -2, SC = 0
  • total_rows = 2−(−2)+1=52 – (-2) + 1 = 52−(−2)+1=5
  • total_cols = 4−0+1=54 – 0 + 1 = 54−0+1=5

Formula:

Address of A[block][row][col] = Base + Size* (total_rows * total_cols * (Block-SB) + +(row−SR) * total_cols+(col−SC))

Solution:

Address of A[6][0][3]=500+3×((6−3)×5×5+(0−(−2))×5+(3−0))

= 500+3 *(3*5*5++2*5+3)

= 500+3*(75+10+3)

= 500+3*88

= 500+264

= 764

The address of arr[6][0][3] in row-major order is 764.

 

3D Array : Column Major Order

In column-major order, the elements of a 3D array are stored one column at a time within each block. Once all columns in a block are stored, it moves to the next block.

The formula for finding the address of an element in a 3D array in column-major order is:

Address of A[block][row][col]=   Base+Size×(total_rows×total_blocks×(col−SC)+(row−SR)×total_blocks+(Block−SB))

Where:

  • Base = Starting address of the array
  • Size = Size (in bytes) of each element in the array
  • Block, row, col = Position of the element within the block, row, and column, respectively
  • SB, SR, SC = Starting (lower) indices of the blocks, rows, and columns
  • total_rows = Total number of rows
  • total_blocks = Total number of blocks

Question:

Given a 3D array arr[2:6, -3:1, 1:5] with:

  • Base address Base = 600
  • Size of each element Size = 4 bytes
  • Find the address of arr[5][0][4] using column-major order.

Solution:

Given:

  • Base = 600
  • Size = 4 bytes
  • Block = 5
  • row = 0
  • col = 4
  • SB = 2, SR = -3, SC = 1
  • total_rows = 1−(−3)+1=51 – (-3) + 1 = 51−(−3)+1=5
  • total_blocks = 6−2+1=56 – 2 + 1 = 56−2+1=5

Formula for Column Major Order:

Address of A[block][row][col] = Base+Size*{(total_rows*total_blocks*(col−SC)+(row−SR)*total_blocks+(Block−SB)}

Solution:

Address of A[5][0][4]=600+4×(5×5×(4−1)+(0−(−3))×5+(5−2))
Address of A[5][0][4]==600+4×(5×5×3+3×5+3)
 Address of A[5][0][4]==600+4×(75+15+3)
Address of A[5][0][4]== 600 + 4 × 93
Address of A[5][0][4]==600+372
Address of A[5][0][4]==972

Answer:

The address of arr[5][0][4] in column-major order is 972.