Join Regular Classroom : Visit ClassroomTech

C standard library function – realloc()


In case of static memory allocation there are some drawbacks like: wastage of memory, less flexibility, permanent allocation of variables etc. To overcome these situations the concept of Dynamic Memory Allocation has been introduced. In dynamic memory allocation the memories can be allocated dynamically at run time.

There 4 library functions that are described under <stdlib.h> (Standard Library Functions)

1. malloc()
2. calloc()
3. realloc()
4. free()

In this article we’ll thoroughly learn about realloc().

What is realloc()?

Realloc stand for Reallocation. At the time of memory allocation using calloc() or malloc(), it may be possible memory is excess or insufficient. So for that reason we need to reallocate memory at the run time. realloc() is used to dynamically reallocate those memory without losing the data.

Like malloc() and calloc(), memory allocation fails if sufficient memory is not provided. Unsuccessful memory allocation returns a NULL pointer (nullptr) to the pointer.

Syntax

ptr = realloc(ptr, (new_size)*(size_of_datatype));

Example

/* C program to understand how realloc() works in DMA */
/* www.codewindow.in */
 
#include <stdio.h>
#include <stdlib.h>
 
// Driver code
int main() {
    int *p, i , d1, d2;      //pointer and the variables are initialized
    printf("Enter size: ");
    scanf("%d", &d1);
     
    p=(int *)calloc(d1, sizeof(int));   // An int array of n elements has been assigned to the pointer
     
    if(p == NULL) {                  // Checking if memory allocation is successful or not
        printf("\nMemory allocation unsuccessful");
        exit(0);
    }
    else {                          // Else memory allocation is successful
        printf("\nMemory allocation successful");
        printf("\nEnter the elements of array: ");
     
        for(i=0; i<d1; ++i)
            scanf("%d", p+i);   // Array elements are taken as input
     
        printf("\nElements of the array are: ");
        for(i=0; i<d1; ++i)
            printf("%d ", *(p+i));  // Print element of the array
            
        printf("\nEnter the new size: ");
        scanf("%d", &d2);
        
        p=realloc(p, d2*sizeof(int)); // Reallocation of memory 
        
        if(p == NULL) {                  // Checking if memory allocation is successful or not
            printf("\nMemory allocation unsuccessful using realloc");
            exit(0);
        }
        else {
            printf("\nMemory allocation successful using realloc");
            printf("\nEnter %d new elements of the array: ", d2-d1);
            
            for(i=d1; i<d2; ++i)    // Input new d2-d1 elements
                scanf("%d", p+i);  
            
            printf("\nThe final array is: ");   // Print the final array
            
            for(i=0; i<d2; ++i)
                printf("%d ", *(p+i));
        }
        free(p);
    }
    return 0;
}

Input

Enter size: 5
Enter the elements of array: 1 2 3 4 5
Enter the new size: 10
Enter 5 new elements of the array: 10 20 30 40 50

Output

Memory allocation successful
Elements of the array are: 1 2 3 4 5
Memory allocation successful using realloc
The final array is: 1 2 3 4 5 10 20 30 40 50 

Explanation

As per the program at first the size of the previously allocated memory block was 5. So size was = [5 * sizeof(int)]=20 bytes. Thereafter, by using the realloc() the size has been reallocated to [10 * sizeof(int)]=40 bytes.



You Missed

Also Checkout