Array Memory Allocation
Array Memory Allocation
● Array Dsicription:
o Array Index: The location of an element in an array has an index, which identifies
the element. Array index starts from 0.
o Array element: Items stored in an array is called an element. The elements can be
accessed via its index.
o Array Length: The length of an array is defined based on the number of elements
an array can store. In the above example, array length is 6 which means that it can
store 6 elements.
● When an array of size and type is declared, the compiler allocates enough memory to hold
all elements of data.
● E.g. an array face [10] will have 10 elements with index starting from 0 to 9 and the memory
allocated contiguously will be 20 bytes (integer = 2 bytes).
● The compiler knows the address of the first byte of the array only. Also, the address of the
first byte is considered as the memory address for the whole array.
Types of Arrays
● One-Dimensional Array
o A one-dimensional array is also called a single dimensional array
o where the elements will be accessed in sequential order.
o This type of array will be accessed by the subscript of either a column or row
index.
● Multi-Dimensional Array
o When the number of dimensions specified is more than one, then it is called as a
multi-dimensional array.
o Multidimensional arrays include 2D arrays and 3D arrays.
● A three-dimensional array
o The array face [5] [10] [15] can hold 750 elements (5 * 10 * 15).
Address Calculation in single (one) Dimension Array:
● Address of an element A[ I] is calculated using the following formula:
● 1100 + (1—0) *4
● 1100 + (4-0) * 4
o Address of A [ I ] = B + W * ( I – LB )
o Where,
B = Base address
W = Storage Size of one element stored in the array (in byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
o Example:
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
● While storing the elements of a 2-D array in memory, these are allocated contiguous
memory locations.
● Therefore, a 2-D array must be linearized so as to enable their storage.
● There are two alternatives to achieve linearization:
o Row-Major
o Column-Major.
0 1 2 3 4 5 6 7 8 9 10 11
Where
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix
● Address of A[ I ][ J ] = B + W * [M*( J – Lc ) + ( I – Lr )]
Where
B = Base address
I = Row subscript of element whose address is to be found
J = Column subscript of element whose address is to be found
W = Storage Size of one element stored in the array (in byte)
Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero)
Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero)
M = Number of row of the given matrix
N = Number of column of the given matrix
● Important Note :
o Usually number of rows and columns of a matrix are given ( like A[20][30] or
A[40][60] ) but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number
of rows and columns are calculated using the following methods:
▪ Number of rows (M) will be calculated as = (Ur – Lr) + 1
Number of columns (N) will be calculated as = (Uc – Lc) + 1
▪ And rest of the process will remain same as per requirement (Row Major
Wise or Column Major Wise).
Examples:
Solution:
As you see here the number of rows and columns are not given in the question. So they are
calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26
int A[15][20];
What would be the address of A[5][5] if storage is Row major or Column major?
(Given the base address = 100)
A[4][4]
Solution:
As you see here the number of rows and columns are not given in the question. So they are
calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26
Single Dimension:
Two Dimensions :
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.
Solution: Example 2:
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.
Given :
B = Base address = 4000
I = Row subscript of element whose address is to be found = 6
J = Column subscript of element whose address is to be found = 8
W = Storage Size of one element stored in the array (in byte) = NOT Given
Lr = Lower limit of row/start row index of matrix = 1
Lc = Lower limit of column/start column index of matrix = 1
M(or R) = Number of row of the given matrix = 15
N (or C) = Number of column of the given matrix = 20
Solution: Example 2:
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.
Given :
B = Base address = 4000
I = Row subscript of element whose address is to be found = 6
J = Column subscript of element whose address is to be found = 8
W = Storage Size of one element stored in the array (in byte) = NOT Given
Lr = Lower limit of row/start row index of matrix = 1
Lc = Lower limit of column/start column index of matrix = 1
M(or R) = Number of row of the given matrix = 15
N (or C) = Number of column of the given matrix = 20
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.
Given :
B = Base address = 1430
I = Row subscript of element whose address is to be found = 3
J = Column subscript of element whose address is to be found = 6
W = Storage Size of one element stored in the array (in byte) = 4
Lr = Lower limit of row/start row index of matrix = -4
Lc = Lower limit of column/start column index of matrix = 3
M(or R) = Number of row of the given matrix = 6 – (-4) + 1 = 11
N (or C) = Number of column of the given matrix = 8 – 3 +1 = 6
Example 3:
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.
Given :
B = Base address = 1430
I = Row subscript of element whose address is to be found = 3
J = Column subscript of element whose address is to be found = 6
W = Storage Size of one element stored in the array (in byte) = 4
Lr = Lower limit of row/start row index of matrix = -4
Lc = Lower limit of column/start column index of matrix = 3
M(or R) = Number of row of the given matrix = 6 – (-4) + 1 = 11
N (or C) = Number of column of the given matrix = 8 – 3 +1 = 6