Memory Address Calculation

Memory Address Calculation

One Dimensional Array (1D Array)

1d array memory calculation

Array of an element inside an array say “item[I]” is calculated by using this formula:

AddressOf(item[I]) = B + ( W * ( I + LB) )

Note:

item[I] :  Address of array element is item[I]

B         :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

I           :  Index / Subscript of array elements whose address is to be found.

LB       :  Lower Bound / First element Index value, if not specified assume 0 (Zero)

Example:

Given the base address of an array item[1100 … 2000] as 1024 and size of each element is 2 bytes in ram. Find the address of item[1500].

Sol.:

Given values are:

  • Base address (B)   : 1024
  • Size (W)                    :  2 bytes
  • Lower Bound (LB)  :  1100
  • Index (I)                   :   1500

AddressOf( item[I] ) = B + ( W * ( I + LB) )

            =  1024 + ( 2 * ( 1500 – 1100) )

            =  1024 + ( 2 * 400 )

            =  1024 + 800

            =  1824

So, item [ 1500 ] address is 1824

Two Dimensional Array (2D Array)

The elements of a 2-D array in memory, these are stored in contiguous memory locations like 1D Array. 2D Array elements are accessed in two ways:

2 d array memory representation

Row Major Order

2D Array can be accessed Row-Wise. The Elements of the 1st row is stored first, then the elements of 2nd row and so on.

Row major order

Row Major Order is calculated using this formula:

AddressOf( item[R][C] ) = B + W * [ n * ( R – LBR ) + (C – LBC) ]

Note: 

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

S          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

n          :  Total number of columns in the array.

LBR     :  Lower Bound of the Row.

LBC     :  Lower Bound of the Column.

Example:

An Array Item[15][10] is stored along the row in the memory with each element requiring 8 bytes of storage. If the base address of array Item is 14000, find out the address of Item[10][7].

Sol.:

Given values are:

  • Item[10][7]              :  ?
  • Base address (B)   :  14000
  • Size (W)                    :  8 bytes
  • n                               :  10
  •                               : 10
  • C                               : 7

AddressOf( item[R][C] ) = B + W * [ n * ( R – LBR ) + (C – LBC) ]

           Item[10][7] =  14000 + 8 * [ 10 ( 10 – 0 ) + ( 7 – 0 ) ]

                             =  14000 + 8 * [ 10 ( 10 ) + 7]

                             =  14000 + 8 * [ 100 + 7 ]

                             =  14000 + 8 * 107

                             =  14000 + 856

                             =  14856

So, Item[10][7] address is 14856

Column Major Order

2D Array can be accessed Column-Wise. The Elements of the 1st column is stored first, then the elements of 2nd column and so on.

column major order

Column Major Order is calculated using this formula:

AddressOf( item[R][C] ) = B + W * [ ( R – LBR ) + m * (C – LBC) ]

Note: 

R          :  The Row number of the element.

C          :  The Column number of the element.

B          :  Base Address / First Address of array element. Both are  same.

W          :  Size / Width in byte. For eg. int (2 byte), float(4 byte) and so on.

m          :  Total number of Rows in the array

LBC     :  Lower Bound of the Column.

LBR     :  Lower Bound of the Row.

Example:

An array Item[20][10] is stored in the memory along the column with each of the elements occupying 2 bytes. Find out the memory address of Item[10][5], if the element Item[2][9] is stored at the address 7600.

Sol.:

Given values are:

  • Item[2][9]                 :  7600
  • Base address (B)   :  ?
  • Size (W)                    :  2 bytes
  • m                              :  20
  •                               :  2
  • C                               :  9

AddressOf( item[R][C] ) = B + W * [ ( R – LBR ) + m * (C – LBC) ]

           7600 =  B + 2 * [ ( 2 – 0 ) + 20 * ( 9 – 0 ) ]

           7600 =  B + 2 *  ( 2 + 180 )

           7600 =  B + 364

                 B =  7600 – 364

                 B = 7236

So, Base address is 7236

  • Item[10][5]              :  ?
  • Base address (B)   :  7236
  • Size (S)                    :  2 bytes
  • m                              :  20
  •                               :  10
  • C                               :  5

          Item[10][5] = 7236 + 2 * [ ( 10 – 0 ) + 20 * ( 5 – 0 ) ]

                            = 7236 + 220 

                            = 7456

Run This Code to Know more about Column Major or Row Major Order

OUTPUT:

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *