Realloc in C – Altering the Size of a Block

It is likely that we discover later, the previously allocated memory is not sufficient and we need additional space for more elements. It is also possible that the memory allocated is much larger than necessary and we want to reduce it. In both the cases, we can change the memory size already allocated with the help of the function realloc in c. This process is called the reallocation of memory. For example, if the original allocation is done by the statement
ptr = malloc(size);
then reallocation of space may be done by the statement
ptr = realloc(ptr, newsize);

This function allocates a new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the new memory block. The newsize may be larger or smaller than the size. Remember, the new memory block may or may not begin at the same place as the old one. In case, it is not able to find additional space in the same region, it will create the same in an entirely new region and move the contents of the old block into the new block. The function guarantees that the old data will remain intact.

If the function is unsuccessful in locating additional space, it returns a NULL pointer and the original block is freed (lost). This implies that it is necessary to test the success of operation before proceeding further. This is illustrated in the program of Program.

Write a program to store a character string in a block of memory space created by malloc and then modify the same to store a larger string.

The program is shown in below realloc in c. The output illustrates that the original buffer size obtained is modified to contain a larger string. Note that the original contents of the buffer remains same even after modification of the original size.

Program
          #include <stdio.h>
          #include <stdib.h>
          #define NULL 0
          main()
          {
               char *buffer;
               /* Allocating memory */
               if((buffer = (char *)malloc(10)) == NULL)
               {
                 printf("malloc failed.\n");
                 exit(1);
               }
               printf("Buffer of size %d created \n" ,_msize(buffer));
               strcpy(buffer, "HYDERABAD");
               /* Reallocation */
               if((buffer = (char *)realloc(buffer,15)) == NULL)
               {
                 printf("Reallocation failed. \n");
                 exit(1);
               }
               printf("\nBuffer size modified. \n");
               printf("\nBuffer still contains: %s \n",buffer);
               strcpy(buffer, "SECUNDERABAD");
               printf("nBuffer now contains: %s \n",buffer);
            /* Freeing memory */
            free(buffer);
            }
Output
            Buffer of size 10 created
            Buffer contains: HYDERABAD
            Buffer size modified
            Buffer still contains: HYDERABAD
            Buffer now contains: SECUNDERABAD
Read More Topics
Accessing Structure Members in C
Nesting of Function in C
Passing Arrays to Function in C
Function Declaration in C
User Defined Function (UDFS)

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply