Topic :Dynamic Memory allocation using Realloc() function in C++ Programming Language:

Realloc():

In Our Previous Tutorials we discuss about Dynamic memory Allocation using Calloc() function. In this tutorial we will discuss, How to allocate Memory dynamically using Realloc() Function. C Library < stdlib > Provide Realloc() function for this Purpose.

Let's suppose, we write a program to store students data. Our program allocate memory dynamically. The program allocate memory for only 20 students. but how about if we need to add 5 more students. because our functions are not able to allocate memory again for more 5 students. so should be terminate our Program and start again to allocate enough memory. The answer is NO. because C Library < stdlib > provide us another function known as Realloc() to resize the allocated memory.




Realloc() function is used to resize the Previously allocated Memory, that was allocated with malloc() or calloc() function.



  • The Reallocation is done by either:
    » Expanding or contracting the existing area to by pointer, if possible. the contents of the area remain unchanged up to the lesser of the new and old sizes. if the area is expended, the contents of the New part of the array are undefined.
    » Allocating a New memory Block of sizes new-sizes bytes. copying memory area with size equal the lesser of the new and old sizes and freeing the old block.



If there is Not enough memory, The old Memory block will not be freed and Null pointer will return.

We can use this function only if the memory is already allocated Dynamically. If the memory is not dynamically allocated, then the behaviour is undefined.



The basic syntax is:


void* realloc(void *ptr, New_size);


In Above Syntax, ptr is the Pointer to old memory, that we need to resize. and New_size is the new size to be allocated. if the realloc() function fail to allocate new memory, then the old memory won't be free and an NULL pointer will return.

if We need pointer to return any other data type instead of Void, then we can use Type cast operator. The syntax is:



cast_type *ptr; //pointer to data type;
ptr = (cast_type *)realloc(ptr, size);

//example:

int *ptr;
ptr=(int*)realloc(ptr,num*sizeof(int));


In Above example, we use sizeof() operator to allocate the exact Memory in bytes. because the realloc() need the size of elements in bytes.



Let's have an example here to know, how exactly we can reallocate the memory using realloc() function.



/* Example: realloc() function: InfoBrother*/

#include<iostream>
#include<stdlib.h> //for realloc and malloc function.
#include<string.h> //for string function.

using namespace std;

main()
{
char *ptr;

/* Initial memory allocation */
ptr = (char *) malloc(20);
if(ptr!=NULL)
{
    strcpy(ptr, "InfoBrother");
    cout<<" The string is: "<<ptr<<" and the Address is: "<<&ptr<<endl;
}

else
cout<<" Error while allocating memory: "<<endl;

/*********************************************/

/* Reallocating memory */
ptr = (char *) realloc(ptr, 25);
if(ptr!=NULL)
{
strcat(ptr, ".com");
cout<<" String is: "<<ptr<<" and address is: "<<&ptr<<endl;
}

else
cout<<" Error while allocating Memory: "<<endl;

free(ptr); //free the memory;

    
}


In Above example, we allocate the memory for the string "InfoBrother" using malloc() function. then we realize that ".com" is missing from the string. so we reallocate the memory for "InfoBrother.com" using realloc() function. we write the error checker code too in our program, and at the end as responsible person, we free the allocated space. that was the perfect example to understand the concept of realloc() function.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement