There could be many situations while programming in C or C++ language when you need to dynamically allocate memory. Normally, the compiler during compilation allocates memory for a program.
In situations where the computer does not know how much memory to allocate, like in the case of array initialization (without predefined array length) both C and C++ programming languages give the user the power to dynamically allocate memory at run-time.
The four functions used for dynamic allocation of memory are,
malloc()
calloc()
realloc()
free()
In C and C++, all the above functions are defined inside the stdlib
and cstdlib
header files. Learn more about malloc() and calloc()
In this tutorial, let’s go over the realloc() function in both languages.
The realloc() function
The realloc()
function dynamically reallocates previously allocated memory(un-freed) as well as resizes it.
Syntax:
ptr = realloc( ptr , new_size );
ptr
– It is the pointer to the first byte of the previously allocated memory. After reallocation, it serves as the pointer to the first byte of the newly allocated memory,new_size
– It is the new size of the memory block for reallocation in bytes.
The realloc()
function returns a void pointer on a successful reallocation of memory. Whereas, returns a Null pointer on failure.
The realloc() function automatically allocates more memory to a pointer as and when required within the program. If a pointer is allocated with 4 bytes by definition and a data of size 6 bytes is passed to it, the realloc() function in C or C++ can help allocate more memory on the fly.
realloc in C
The following code illustrates the use of realloc()
in C.
#include<stdio.h> #include<stdlib.h> //required for using realloc() in C int main() { int *ptr; int i; // typecasting pointer to integer ptr= (int *)calloc(4,sizeof(int)); if(ptr!=NULL) { for(i=0;i<4;i++) { printf("Enter number number %d: ", i+1); scanf("%d",(ptr+i)); } } //reallocation of 6 elements ptr= (int *)realloc(ptr,6*sizeof(int)); if(ptr!=NULL) { printf("nNew memory allocated!n"); for(;i<6;i++) { printf("Enter new number %d: ",i); scanf("%d",(ptr+i)); } } printf("nnThe numbers are:n"); for(i=0;i<6;i++) { printf("%d n",ptr[i]); } free(ptr); return 0; }
Output:
Understanding the code: In the above code, we have tried to solve a simulated real-world situation using the realloc()
function in C.
As you can see, after the declaration of an integer pointer, ‘ptr’ and an integer iterator ‘i’, we have dynamically allocated memory using calloc()
for 4 sets of integer type data.
With ptr pointing to the first memory location. Then we have checked for successful memory allocation using the condition ptr!=NULL
.
After the successful allocation of memory, we took user input for the number of their choice. These numbers are the specific values or data stored inside the memory spaces allocated by the calloc()
function.
But after this, we get another 2 numbers to store. In that case, we use the realloc()
function and reallocate memory space for a total of 6 sets of data. After successful reallocation, the previous 4 data stored stay intact and untouched. So, accordingly, we take user input for the 2 new data.
Certainly, to check whether all our data have been stored, we try to print all of them using the pointer, ptr
. After that, we yet again use realloc(ptr,0)
to free all the memory locations that we have used. Simply, realloc(ptr,0)
does the same task free(ptr)
does.
In the next section, we have followed the same logic using C++ programming.
realloc() in C++
The below-given code illustrates the use of realloc()
in C++.
#include<iostream> #include<cstdlib> // for using realloc() in C++ using namespace std; int main() { int *ptr; int i; //allocation of memory for 4 elements ptr= (int *)calloc(4,sizeof(int)); if(ptr!=NULL) { for(i=0;i<4;i++) { cout<<"Enter number "<<i<<":"; cin>>ptr[i]; } } //reallocation of memory ptr= (int *)realloc(ptr,6*sizeof(int)); if(ptr!=NULL) { cout<<"nNew memory allocated!n"; for(;i<6;i++) { cout<<"Enter new number "<<i<<":"; cin>>ptr[i]; } } cout<<"nnThe numbers are:n"; for(i=0;i<6;i++) { cout<<ptr[i]<<endl; } free(ptr); return 0; }
The output will be the same as the image above. The only difference is in the code which is now written in C++.
Conclusion
So in this article, we learned how to use the realloc() function in C as well as in C++. We also discussed the respective code for better understanding.