The realloc() Function in C
Last updated on July 27, 2020
Let's say we have allocated some memory using malloc()
and calloc()
, but later we find that memory is too large or too small. The realloc() function is used to resize allocated memory without losing old data. It's syntax is:
Syntax: void *realloc(void *ptr, size_t newsize);
The realloc()
function accepts two arguments, the first argument ptr
is a pointer to the first byte of memory that was previously allocated using malloc()
or calloc()
function. The newsize parameter specifies the new size of the block in bytes, which may be smaller or larger than the original size. And size_t
is just an alias of unsigned int
defined inside stdlib.h
header file.
Let's take an example:
1 2 | int *p;
p = (int*)malloc(5*sizeof(int)); // allocate memory for 5 integers</pre>
|
Suppose that sometimes later we want to increase the size of the allocated memory to store 6
more integers. To do that, we have to allocate additional 6 x sizeof(int)
bytes of memory. Here is how you will call realloc()
function to allocate 6 x sizeof(int)
bytes of memory.
1 2 | // allocate memory for 6 more integers integers i.e a total of 11 integers
p = (int*)realloc(p, 11*sizeof(int));
|
If sufficient memory (in this case 6 * sizeof(int)
bytes) is available following already used bytes then realloc()
function allocates only allocates 6 * sizeof(int)
bytes next to already used bytes. In this case, the memory pointed to by ptr
doesn't change. It is important to note that in doing so old data is not lost but newly allocated bytes are uninitialized.
On the hand, if sufficient memory (in this case 6 * sizeof(int)
bytes ) is not available following already used bytes then realloc()
re-allocates entire 11 * sizeof(int)
bytes of memory somewhere else in the heap and copies the content from the old memory block to the new memory block. In this case, address pointed to by ptr
changes.
If realloc()
failed to expand memory as requested then it returns NULL
, the data in the old memory remains unaffected.
The following program demonstrates the realloc()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include<stdio.h>
#include<stdlib.h>
int main()
{
int *p, i, n;
printf("Initial size of the array is 4\n\n");
p = (int*)calloc(4, sizeof(int));
if(p==NULL)
{
printf("Memory allocation failed");
exit(1); // exit the program
}
for(i = 0; i < 4; i++)
{
printf("Enter element at index %d: ", i);
scanf("%d", p+i);
}
printf("\nIncreasing the size of the array by 5 elements ...\n ");
p = (int*)realloc(p, 9 * sizeof(int));
if(p==NULL)
{
printf("Memory allocation failed");
exit(1); // exit the program
}
printf("\nEnter 5 more integers\n\n");
for(i = 4; i < 9; i++)
{
printf("Enter element at index %d: ", i);
scanf("%d", p+i);
}
printf("\nFinal array: \n\n");
for(i = 0; i < 9; i++)
{
printf("%d ", *(p+i) );
}
// signal to operating system program ran fine
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Initial size of the array is 4
Enter element at index 0: 11
Enter element at index 1: 22
Enter element at index 2: 33
Enter element at index 3: 44
Increasing the size of the array by 5 elements ...
Enter 5 more integers
Enter element at index 4: 1
Enter element at index 5: 2
Enter element at index 6: 3
Enter element at index 7: 4
Enter element at index 8: 5
Final array:
11 22 33 44 1 2 3 4 5
|
Load Comments