Allocate space for an array
Storing single values on the heap can be useful, but often you want to be able to allocate enough space for a number of values. This uses the same memory allocation functions, but requires you to pass in the size of the type you want multiplied by the number of elements you want.
In C/C++
In C you can use malloc
to allocate space for a number of elements. Alternatively the calloc
function from stdlib.h
provides direct support for allocating space for an array. This function takes two parameter, the first takes the number of elements to allocation, the second the size of those elements. calloc
also clears its allocation, setting each byte allocated to 0.
The following code allocates 4 integer values as shown in Figure x.y: p = (int*) malloc(4 * sizeof(int));
or using calloc
, which would set all values to 0, you could use p = (int*) calloc(4, sizeof(int));
In C you can use the standard array access mechanisms with pointers to access subsequent elements. So p[0]
is the value in the first element of the array dynamically allocated in Figure x.y, p[1]
is the value of the second element, p[2]
is the value of the third element, and so on. This relates back to pointer arithmetic discussed when we first looked at pointers, and mirrors the way we used arrays when passed to parameters.
Array Allocation: Why, When, and How
Arrays on the stack must be of a fixed length, whereas with the heap there is the capacity to change the size of a memory allocation. This means you can use the heap to create variable length arrays, where you can add and remove elements from the array and have its size in memory change.
Example
The following example demonstrates how to allocate space for an array of 4 elements using malloc
. The example code to use calloc
is shown in a comment, and produces the same results as using malloc
but with the guarantee that all values are set to 0.
Navigate the slide-show below which details the steps for allocating space for an array of 4 elements.