C – Calloc() function

realloc() :

  • It is used to increase/decrease the size of array which is created by calloc()
  • Using calloc(), we can create the size with fixed size
  • Dynamic memory : Size varies depends on requirement
  • We can modify the size of array using realloc() function

void* realloc(void* ptr, size_t size);

#include<stdlib.h>
void main(){
            int *p1, *p2, m, n;
            printf(“enter size of array : “);
            scanf(“%d”,&m);
 
            p1 = (int*)calloc(m,sizeof(int));
            if(p1 == NULL){
                        printf(“calloc failed\n”);
                        exit(1);
            }
            else
                        printf(“memory allocated..\n”);
 
            printf(“Enter new size of array : “);
            scanf(“%d”,&n);
           
            p2=(int*)realloc(p1 , n*sizeof(int));
            if(p2 == NULL){
                        printf(“realloc failed\n”);
                        exit(1);
            }
            else
                        printf(“memory re-aquired..\n”);
            free(p1);
            free(p2);
}

free() :

  • The memory which has allocated dynamically must be released explicitly using free function.
  • Function prototype is:
    • void free(void* ptr);
  • Using free() unction , we can de-allocate the memory of any type of pointer.

Scroll to Top