realloc() Function
The realloc()
function in C dynamically adjusts the size of an allocated memory block. It enables you to either expand or shrink a block of memory while preserving the existing data up to the minimum of the old and new sizes. The memory block may be moved to a new location during this process.
Syntax of realloc()
void* realloc(void* ptr, size_t size);
Parameters
Parameter | Description |
---|---|
ptr | Pointer to a memory block previously allocated by malloc() , calloc() , or realloc() . It can also be NULL , in which case realloc() behaves like malloc() . |
size | New size for the memory block in bytes. |
Return Value
The function returns a pointer to the reallocated memory block, which might be the same as the original pointer or a new location. A return value of NULL
indicates that either the requested size was zero (resulting in deallocation) or the memory allocation failed, in which case the original memory block remains unchanged.
Additional points: If the new size is larger than the original, the additional memory will have indeterminate values. Also, if realloc()
fails to allocate the requested memory, the original block is not freed.
Examples for realloc()
Example 1: Expanding a Dynamically Allocated Array
This example demonstrates how to expand an existing memory block to accommodate additional elements.
Program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *arr = malloc(5 * sizeof(int));
if (arr == NULL) {
return 1;
}
// Initialize array with values 1 to 5
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
// Expand array to hold 10 integers
int *temp = realloc(arr, 10 * sizeof(int));
if (temp == NULL) {
free(arr);
return 1;
}
arr = temp;
// Initialize the new elements to 0
for (int i = 5; i < 10; i++) {
arr[i] = 0;
}
// Print the expanded array
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Explanation:
- Allocated memory for an array of 5 integers and initialized it with values from 1 to 5.
- Used
realloc()
to expand the memory block to hold 10 integers. - Initialized the additional elements (indices 5 to 9) to 0.
- Printed the entire array to verify that the original data was preserved and the new elements were set correctly.
Program Output:
1 2 3 4 5 0 0 0 0 0
Example 2: Reducing the Size of an Allocated Memory Block
This example shows how to reduce the size of an allocated memory block while preserving the initial portion of the data.
Program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *arr = malloc(10 * sizeof(int));
if (arr == NULL) {
return 1;
}
// Initialize array with values 1 to 10
for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
}
// Reduce array size to hold only 5 integers
int *temp = realloc(arr, 5 * sizeof(int));
if (temp == NULL) {
free(arr);
return 1;
}
arr = temp;
// Print the reduced array
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Explanation:
- Allocated memory for an array of 10 integers and initialized it with values from 1 to 10.
- Used
realloc()
to shrink the memory block to hold only 5 integers. - Printed the first 5 elements to verify that the original data was preserved.
Program Output:
1 2 3 4 5