Reallocate memory
The advantage of dynamic memory allocation is that you can change your allocations. If you asked for an array of two values, you may later want to be able to expand that array to three or four elements. Alternatively, an array with twenty elements may have some data removed and be shrunk down to only 5 elements. All of this is possible with dynamic memory allocation. You can ask to have the memory you were allocated changed to a different size.
Realloc - Why, When, and How
You can use realloc when you have an existing heap allocation that you want to change. You may need more space, or have reduced the size you need. In either case the realloc call can let you change the allocation.
In C/C++
Like malloc
and calloc
, realloc
allows you to allocate space from the heap. Using realloc
you can change (reallocate) space on the heap, by passing in the existing pointer and the new size.
Function | Arguments | Returns | Description |
---|---|---|---|
realloc | the pointer to reallocate, and new size in bytes. | a pointer (void * ) to the new address | Changes the memory allocation and returns a pointer to it. |
As with the other memory allocation functions, you need to import the stdlib.h header file. The realloc function has the following prototype:
To use this safely you need to think about how it works:
- If it can reallocate at the same location, it will and returns the original pointer.
- If needed to move the allocation, it will do and returns the new address of the data.
- When you are out of memory, or it cannot find a new place to store the data, it will return
nullptr
/NULL
.
To handle all three of these cases you need to make sure you do not update the original pointer value until you have checked if the realloc worked. If you get back NULL, then you need to have kept the original pointer so that you can get back to the space on the heap, and potentially free it as you are out of space in memory.
Example
The following example shows how to use realloc
to both allocation and then change the memory allocation. This would work equally well if you used malloc
or calloc
to do the original memory allocation.
Notice how we use an if statement to check if the realloc worked. If it did, we update the pointer, otherwise it retains its original value.
To make this work in reality we would need to put this in a struct where we also keep track of the size of the array as we have before.
Realloc (allocation at original address)
Navigate the slide-show below to understand how realloc works when the memory reallocation happens to expand at the original memory address
Realloc (allocation at new address)
Navigate the slide-show below to understand how realloc works when the memory reallocation happens to shift from it’s original memory space to a new area
Reallocating multidimensional arrays
If you need to change the allocation of a multidimensional array, the implications will depend on how this is implemented. If you have a ragged array, then you can reallocate each of the points, giving you the ability to resize different parts of the array as you go. With a rectangular array, you can resize associated memory and then move values from their old positions to their new positions. When you do this, you need to make sure you do this from the end of the old array back to the start.