Lesson 9.4. How C arrays are stored in memory?

Storing arrays in memory

The following properties are guaranteed when storing an array:

This implies that an array is stored as a single block in memory. It is not possible that the array is scattered in several places in the memory. In the same way each cell precedes its next in memory. The cell tab[5] is stored between cells tab[4] and tab[6].

Organization of C arrays in memory

In the following example, we create an array of characters and display the address of each cell of the array. The display confirms that the cells of the array are consecutive: the addresses follow each other.

Dominant columns or rows

In computer science, there are two storage policies for multidimensional arrays:

Multidimensional arrays

A C compiler will always organize arrays in row-major order. Consider the following array:

int tab[3][4]

Here is how the array is stored in memory:

Memory for a multidimensional array in C

Segmentation fault

A segmentation fault is the crash of an application that has attempted to access a memory location that was not allocated to it. Let's consider, for example, the following array:

int tab[10];

It is an array of 10 cells, numbered from 0 to 9. What happens if you write beyond the 10th cell?

tab[218] = 666;

The compiler detects errors only during the transformation from the source code into machine language. So writing to an element in an undeclared cell of an array is feasable. Depending on the context, here is what can happen:

Segmentation fault while writing out of an array in C

A classic mistake is to write in the (N+1)th cell of an N cells array:

int tab[10];
tab[10] = -1; // <= Segmentation fault

Exercises

Exercise 1

Write a program that displays all the cell addresses of the 2-dimensional table below:

char matrix[3][4];

The expected display is the following, we can see that all the addresses follow each other:

Address of matrix[0][0]:    0x7ffcab45f148
Address of matrix[0][1]:    0x7ffcab45f149
Address of matrix[0][2]:    0x7ffcab45f14a
Address of matrix[0][3]:    0x7ffcab45f14b
Address of matrix[1][0]:    0x7ffcab45f14c
Address of matrix[1][1]:    0x7ffcab45f14d
Address of matrix[1][2]:    0x7ffcab45f14e
Address of matrix[1][3]:    0x7ffcab45f14f
Address of matrix[2][0]:    0x7ffcab45f150
Address of matrix[2][1]:    0x7ffcab45f151
Address of matrix[2][2]:    0x7ffcab45f152
Address of matrix[2][3]:    0x7ffcab45f153

Exercise 2

The code below declares an array of 100 cells and displays its address (address of the first cell). Increase the size of the array until there is not enough memory to create the array. What happens?

int tab[100];
printf ("%p\n", &tab); // &tab is equivalent to &tab[0], address of the first cell

Exercise 3

We provide the declaration of the array prime:

int prime[5]={2,3,5,7,11};

Write a program that asks the user for the index to be displayed. Then the program displays the cell corresponding to to the entered index. Test the program with values greater than 4.

Index of the cell to display: 4
prime[4] = 11
Index of the cell to display: 8
prime[8] = 4195824
Index of the cell to display: 456465461
signal: segmentation fault (core dumped)

Quiz

In C, arrays are stored ...

Check Bravo! A C compiler will always organize arrays in row-major order. Try again...

Let's consider the following array. In memory, which cell will follow the cell carre[2][4]?

char square[10][10];
Check Bravo! Check this section if you are not convinced. Try again...

If there is not enough memory in one block to store an array, what will happen?

unsigned char image[200][300][3];
Check Bravo! The array is not created if there is not enough memory. Try again...

What is a segmentation fault?

Check Bravo! A segmentation fault is raised when a software has attempted to access a restricted area of memory (a memory access violation). Try again...

See also


Last update : 11/24/2022