Syntax of realloc()
Syntax:
void *realloc(void *ptr, size_t size);
So, The function needs two arguments:
- **(void *ptr): A pointer that points to the beginning of a memory block that we want to resize.
- (size_t size): This represents the new size of the block(in bytes), where size_t is just an alias of unsigned int, defined in the library of C. The new block can be smaller or larger than the original block.
Realloc deallocates the original object pointed to by ptr and returns a pointer to a new object. The contents of the new object are identical to that of the original object prior to deallocation.
Example
Example 1
Let’s understand these with the help of an example:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
ptr_new = (int *)realloc(ptr, sizeof(int)*3);
*(ptr_new + 2) = 30;
for(i = 0; i < 3; i++){
printf("%d ", *(ptr_new + i));
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
10 20 30
In this example, firstly, we want ptr to store the space of 2 integers, and then we store 10 and 20. Now we are deallocating the memory, and now we want to store the space of 3 integers, and then we store 30 in it. So it will simply print 10 20 30.
Also see, Tribonacci Series and Short int in C Programming
Example 2
C
#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 3 elements ...\n ");
p = (int*)realloc(p, 7 * sizeof(int));
if(p==NULL)
{
printf("Memory allocation failed");
exit(1); // exit the program
}
printf("\nEnter 3 more integers\n\n");
for(i = 4; i < 7; i++)
{
printf("Enter element at index %d: ", i);
scanf("%d", p+i);
}
printf("\nFinal array: \n\n");
for(i = 0; i < 7; i++)
{
printf("%d ", *(p+i) );
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Original size of the array is 4
Enter element at index 0: 99
Enter element at index 1: 88
Enter element at index 2: 77
Enter element at index 3: 66
Increasing the size of the array by 3 elements ...
Enter 3 more integers
Enter element at index 4: 10
Enter element at index 5: 20
Enter element at index 6: 30
Final array:
99 88 77 66 10 20 30
In this example, initially, our array size is 4, so we can store up to 4 integers, and after that, we use the relloc() function to reallocate the memory keeping the original content same, so using relloc() function, our array size is 7, so we added 3 more integers. After that, we print all the elements stored in the array.
You can also read about the dynamic arrays in c and Data Structure.
Frequently Asked Questions
What is the realloc method?
The realloc function dynamically adjusts the size of previously allocated memory, preserving existing data, or returns NULL if resizing fails.
What is realloc int in C?
Using realloc with an integer pointer adjusts the memory block size for integers, allowing dynamic resizing while maintaining previously stored integer values.
What is realloc vs calloc in C?
calloc allocates and initializes memory to zero, while realloc resizes existing memory blocks, retaining or reallocating data as needed.
Conclusion
In this article, we have discussed the use of realloc function.We also discussed its syntax with examples.
Enhance your skills in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and more with our Code360 Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Code360! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problems, interview experiences, and interview bundle for placement preparations.
You may consider our paid courses curated by experts to give your career an edge over others!