C Programming language

adplus-dvertising
Array Representation in Memory
Previous Home Next

How arrays are represented in the memory ?

Mainly arrays are categorized in

  • One dimensional array
  • Multidimensional array

One dimensional arrays are the simple arrays that we have so far discussed. In previous examples we have shown how to initialize and declare one dimensional array, now we will discuss how it is represented in the memory. As we know now 1-d array are linear array in which elements are stored in the successive memory locations. The element at the very first position in the array is called its base address. Now consider the following example :

int arr[5];

Element 34 78 98 45 56
Index arr[0] = 100 arr[1] = ? arr[2] = ? arr[3] = ? arr[4] = ?

Here we have defined an array of five elements of integer type whose first index element is at base address 100. i.e, the element arr[0] is stored at base address 100. Now for calculating the starting address of the next element i.e. of a[1], we can use the following formula :

Base Address (B)+ No. of bytes occupied by element (C) * index of the element (i)

/* Here C is constant integer and vary according to the data type of the array, for e.g. for integer the value of C will be 4 bytes, since an integer occupies 4 bytes of memory. */

Now, we can calculate the starting address of second element of the array as :

arr[1] = 100 + 4 * 1 = 104/*Thus starting address of second element of array is 104 */

Similarly other addresses can be calculated in the same manner as :

arr[2] = 100 + 4 * 2 = 108

arr[3] = 100 + 4 * 3 = 112

arr[4] = 100 + 4 * 4 = 116

Multidimensional arrays are often known as array of the array. In multidimensional arrays the array is divided into rows and columns, mainly while considering multidimensional arrays we will be discussing mainly about two dimensional arrays and a bit about three dimensional arrays. In 2-D array we can declare an array as :

int arr[3][3];

where first index value shows the number of the rows and second index value shows the no. of the columns in the array. We will learn about the 2-D array in detail in the next section, but now emphasize more on how these are stored in the memory.

Mainly multidimensional arrays are stored in the memory in the following two ways :

  • Row-Major order Implementation
  • Column-Major order Implementation

In Row-Major Implementation of the arrays, the arrays are stored in the memory in terms of the row design, i.e. first the first row of the array is stored in the memory then second and so on. Suppose we have an array named arr having 3 rows and 3 columns then it can be stored in the memory in the following manner :

arr[0][0] arr[0][1] arr[0][2]
arr[1][0] arr[1][1] arr[1][2]
arr[2][0] arr[2][1] arr[2][2]

Thus an array of 3*3 can be declared as follows :

arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

and it will be represented in the memory with row major implementation as follows :

1 2 3 4 5 6 7 8 9

In Column-Major Implementation of the arrays, the arrays are stored in the memory in the term of the column design, i.e. the first column of the array is stored in the memory then the second and so on. By taking above eg. we can show it as follows :

arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

and it will be represented in the memory with column major implementation as follows :

1 4 7 2 5 8 3 6 9
Previous Home Next