Dynamic Memory Allocation

realloc() Functions:

  • When you have to modify the size of previous allocated memory then you can do by realloc function.
  • realloc() function modifies the size and return pointer to the first byte of the new memory block

Syntax:realloc()
realloc(ptr_name, new_size);

Let's use realloc function but before we will allocate the memory.let's uderstand it by doing program.

  • Q:create a array of 10 students for storing rollno ,take rollno from the user and print the rollno.Create array dynamically Using calloc().
  • Solution:
    #include<stdio.h>
    void main()
    {
    int i;
    int *p;
    p = (int*)calloc(10,sizeof(int));
    
    for(i=0;i < 10;i++)
     {  
        printf("Enter the student roll no:");
        scanf("%d",p+i);
        
     }
    }
    

    let's print the roll no on the screen to see what is the current state
    printf("\nRoll numbers are : ");
    for(i=0;i < 10;i++)
     {  
         printf("%d\t",*(p+i));
    
     }
    

    Enter the student roll no:10
    Enter the student roll no:20
    Enter the student roll no:30
    Enter the student roll no:40
    Enter the student roll no:50
    Enter the student roll no:60
    Enter the student roll no:70
    Enter the student roll no:80
    Enter the student roll no:90
    Enter the student roll no:100

    Rollno numbers of 10 students :10    20    30    40    50    60    70    80    90    100

    As we have store the rollno of 10 student but now we have to store rollno of 15 student.So, we have to modify the size of memory block by using realloc() function

    Modifying the memory:
    int *p1 
    p1 = realloc(p, 15);
    





  • Now we will take rollno for remaining 5 students and we will print rollno for all 15 sudents

  • But How to store rollno for 11th student
  • this can be done by using the value of i which has become to 11 so we will use it.
  • //value of  'i' varaible  has become 11
    //so we are using that value for storing rollno for 11_th student
    
    for(j=i;j < 15;j++)
    {
        printf("\nEnter the element:");
        scanf("%d",p1+j);
    }
    
    //printing the elements of array.
    printf("\nArray Elements are after reallocating the size.:");
    for(i=0;i < 15;i++)
    {
        printf("%d\t",*(p1+i));
    
    }
    

    Output:
    Enter the student roll no:101
    Enter the student roll no:102
    Enter the student roll no:103
    Enter the student roll no:104
    Enter the student roll no:105

    Rollno numbers of 15 students: 10    20    30    40    50    60    70    80    90    100    101    102    103    104    105


    Let's do another program.

  • Q:Create a array of float type of 10 elements ,and store the temperature of past 10 days and print them on the screen.Create array dynamically by using calloc() after that reallocate memory for 15 elements .
  • Solution:allocaing memory for 10 elements
    #include<stdio.h>
    void main()
    {
    int i;
    float *p;
    p = (flaot*)calloc(10,sizeof(float));
    
    
    
    for(i=0;i < 10;i++)
     {   printf("Enter the Temperature:");
        scanf("%f",p+i);
     }
     
     
     //Printing the temperatures
    printf("\ntemperature of past 10 days was: ");
    for(i=0;i<10;i++)
     {
        printf("%f\t",*(p+i));
     }
    }
    

    Current state:
    Enter the Temperature:15.5
    Enter the Temperature:20.3
    Enter the Temperature:31.5
    Enter the Temperature:30.1
    Enter the Temperature:35.5
    Enter the Temperature:31.2
    Enter the Temperature:29.6
    Enter the Temperature:30
    Enter the Temperature:25.5
    Enter the Temperature:21.9

    Temperature Of 10 DAYS: 15.5    20.3    31.5    30.1    35.5    31.2    29.6    30.000    25.5    21.9

    Now we will reallocate the memory by realloc()
    We have to increase the size by 5

    reallocating memory::
    float *p1 
    p1 = realloc(p, 15);
    

    //value of  'i' varaible  has become 11
    //so we are using that value for storing rollno for 11_th student
    
    for(j=i; j < 15;j++)
    {
        printf("\nEnter the element:");
        scanf("%f",p1+j);
    }
    
    //printing the elements of array.
    printf("\nArray Elements are after reallocating the size.:");
    for(i=0;i < 15;i++)
    {
        printf("%f\t",*(p1+i));
    
    }
    

    Output:
    Enter the Temperature:16.5
    Enter the Temperature:21.3
    Enter the Temperature:32.5
    Enter the Temperature:20.1
    Enter the Temperature:31.5

    Temperature Of past 15 DAYS are: 15.5    20.3    31.5    30.1    35.5    31.2    29.6    30.000    25.5    21.9    16.5    21.3    32.5    20.1
        31.5



    free() Function:

    • when there is no need to store data and you want to release(delete) that data then you can do that work by free()
    • Syntax:free()
      free(address);  
      

    Let's free the pointer 'p' which we have used in above function:
    free(p); //memory has released
    

    Let's free the pointer 'p1' which we have used in above function:
    free(p1);  //memory has released
    






  • This is how you can reallocate and release the memory.
  • So do more programs for Understanding the concept more better.