Skip to content

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.

Figure x.y: You can ask to be allocated a number of values

You can ask to be allocated a number of values

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.

#include <stdlib.h>
#include <stdio.h>
// Define the size we will use for this array
const int ARR_SIZE = 4;
int main()
{
int *p;
// get space for four integers on the heap
p = (int *)malloc(ARR_SIZE * sizeof(int));
// or with calloc using:
// p = (int *)calloc(ARR_SIZE, sizeof(int));
// Access the data from the heap...
for(int i = 0; i < ARR_SIZE; i++)
{
printf("The value on the heap is %d.\n", p[i]);
}
// Assign the value 10 x i to each element on the heap
for(int i = 0; i < ARR_SIZE; i++)
{
p[i] = 10 * i;
}
// Access the data from the heap
for(int i = 0; i < ARR_SIZE; i++)
{
printf("The value on the heap is now %d.\n", p[i]);
}
// free all space allocated
free(p);
p = nullptr;
return 0;
}

Navigate the slide-show below which details the steps for allocating space for an array of 4 elements.


We begin by declaring a constant variable ARR_SIZE which defines our array size. This variable is stored on the stack.
The program begins at main() and a pointer to an integer is declared as the first step. This variable is also allocated on the stack.
calloc() is called, which assigns enough memory space for an array of ARR_SIZE integers on the heap (in this case a 4-integer array). calloc() will also initialize the space to 0
A for loop runs, iterating over the elements in our array, and printing the initial value of each element to the terminal. NOTE: That we can use array notation p[i] to access elements in a memory region allocated by malloc, calloc or realloc. p[i] is equivalent to *(p + i) (which accesses the memory region using pointer notation instead of array notation)Since p is the memory address of the start of the memory region, (p+i) refers to an offset from that start point, where i refers to the i-th element. The actual offset in bytes is determined by the system using the formula i*sizeof(int)Although we can use this pointer notation, it's much more convenient to use the array notation
Next, another for loop runs, iterating over the elements in our array, and setting the value of each element of our array to the value of the loop counter i multiplied by 10. You can see that the values are set to 0, 10, 20 and 30 respectively in the space on the heap
The third (and last) for loop then runs, and once again iterates over our array elements on the heap, printing out their values to the terminal
Line 18 calls the <strong>free</strong> function, passing it the pointer p. This will free the space allocated on the heap for the memory area that was allocated to the pointer p. p then becomes a dangling pointer
p is then assigned the null pointer value (nullptr), cutting it's ties to the heap space that was allocated previously
Finally, the program returns 0 and ends