In this blog, we will look at the base address of a two dimensional array and how to compute the base address of any element in a two-dimensional array in C++.
What is a Two-Dimensional Array?
In C++, a two dimensional array is a collection of elements arranged in a grid pattern, where each element is indexed by two indices, one for the row and one for the column. The base address of a two dimensional array is the memory address of the first element of the array, which is the element at the top-left corner of the array.
It is a sort of multidimensional array. Declaring a two-dimensional array uses the same syntax as displaying a one-dimensional array, int arr[m][n], where m is the number of rows and n is the number of columns.
The base address of a two dimensional array can be obtained by using the name of the array without any indices. For example, if we have a two dimensional array named ‘arr’ with m rows and n columns, we can obtain the base address of the array using the following syntax:
int arr[m][n];
int *base_address = &arr[0][0];
The above image shows the two-dimensional array, the elements are organized in rows and columns. The first element of the first row is represented by arr[0][0] where the number shown in the first index is the number of that row and the number shown in the second index is the number of the column.
How to Calculate the Base Address of any Element in a Two-Dimensional Array?
To understand how to calculate the base address for elements of a two-dimensional array, we must first understand how a two-dimensional array is stored in memory.
Even though a two-dimensional array appears to be substantially different from a one-dimensional array in C++, the way they are kept in memory is not. A 2-d array’s two-dimensional structure is just provided for the user’s convenience to give it a matrix-like structure. These two-dimensional arrays are actually kept in the memory similarly to one-dimensional arrays.
So, to store a two-dimensional array in memory we need to map the elements into a one-dimensional array and then they are stored in a contiguous manner in the memory for easy access.
There are two main techniques for storing two-dimensional array elements in memory:
- Row major order
- Column major order
Row Major Order
In row-major ordering, all the rows of the two-dimensional array are stored in the memory contiguously. The memory allocation for the array seen in the preceding image is shown in row-major order as follows:
The first row of the array is completely saved in memory, followed by the second row of the array, and so on until the last row of the array is completely recorded in memory.
Column Major Order
According to the column-major ordering, all the columns of the two-dimensional array are stored in the memory contiguously. The memory allocation for the array seen in the preceding image is shown in column-major order as follows:
The first column of the array is completely saved in memory, followed by the second column of the array, and so on until the last column of the array is completely recorded in memory.
Calculating the Base Address of a Random Element of a Two-Dimensional Array
Because there are two methods for storing the two-dimensional array in memory, there are two formulas for calculating the base address of a random element of the 2D array.
By Row Major Order
If the two-dimensional array is defined as arr[m][n], where m is the number of rows and n is the number of columns, the address of an element arr[i][j] of the array stored in row-major order is estimated as
Address(arr[i][j]) = Base Address of two dimensional array + (i*n + j)* size
where the Base address of the two dimensional array is the address of the array’s first member arr[0][0].
By Column Major Order
If the array is defined as arr[m][n], where m is the number of rows and n is the number of columns, the base address of an element arr[i][j] of the array stored in column-major order is calculated as
Address(arr[i][j]) = ((j*m)+i)*Size + Base Address of two dimensional array
where the base address of the two dimensional array is the address of the array’s first member arr[0][0].
Summary
- A two-dimensional array is an array of arrays in which each member is itself an array, resulting in a matrix-like structure.
- The base address of a two dimensional array or of an array is a starting point from which the addresses of all the array’s elements can be computed.
- To store a two-dimensional array in memory, we must first map the elements into a one-dimensional array, which is then stored in memory continuously for simple access.
- There are two methods for mapping a two-dimensional array’s base address into a one-dimensional array for storage:
- Row Major Order: In memory, rows are arranged sequentially one after the other.
- Column Major Order: In memory, columns are sorted consecutively one after the other.