Dynamic Memory Allocation

Calloc() Function

  • When calloc is used to allocate a block of memory, the allocated region is initialized to zeroes.
  • In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values.
  • This could potentially be a security risk because the contents of memory are unpredictable and programming errors may result in a leak of these contents.
  • Allocates space for an array of elements ,initialize them to zero and then return pointer to the memory.

Syntax:Calloc()
(type_Cast*) calloc(no_of _blocks, size_of_datatype);

allocating memory to integer pointer Using calloc():
#include<stdio.h>
void main()
{
int *p;

p = (int*)calloc(1,sizeof(int));

}

allocating memory to float pointer Using calloc():
#include<stdio.h>
void main()
{
float *p;

p = (float*)calloc(1,sizeof(float));

}

allocating memory to char pointer Using calloc():
#include<stdio.h>
void main()
{
char *p;

p = (char*)calloc(1,sizeof(char));

}

Now we will create array by allocating memory dynamically by calloc()

allocating memory for 10 blocks of int type
#include<stdio.h>
void main()
{
int *p;
p = (int*)calloc(10,sizeof(int));
//it will allocate 40 bytes
//it will return pointer to the memory.

}

  • Q:create a array of 10 students for storing rollno ,take rollno from the user and print the temperature.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);
        
     }
    }
    
  • As p is a pointer which has address of first element , so at every iteration we are adding i value to get address of next block.
  • At every iteration we are storing value at p+i address which is address of next block
  • let's print the roll no on the screen
    printf("\nRoll numbers are : ");
    for(i=0;i < 10;i++)
     {  
         printf("%d\t",*(p+i));
    
     }
    

    Output:
    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 are :10    20    30    40    50    60    70    80    90    100



  • 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()
  • Solution:
    #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));
     }
    }
    

    Output:
    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

    Rollno numbers are :15.5    20.3    31.5    30.1    35.5    31.2    29.6    30.000    25.5    21.9



  • Q:Create an array of 10 integer number ,take an another integer number 'n' and array elements of from the user.Find the occurence of number 'n' and print the occurence of number 'n' on the screen .Create array dynamically by using calloc()
  • Solution:
    #include<stdio.h>
    void main()
    {
    int i,n,count=0;
    int *p;
    
    //allocating memory dynamically.
    p = (int*)calloc(10,sizeof(int));
    
    
    printf("\nEnter the number:");
    scanf("%d",&n);
    
    
    //taking element as input.
    for(i=0;i < 10;i++)
    {
        printf("\nEnter the element:");
        scanf("%d",p+i);
    }
    
    //printing the elements of array.
    printf("\nArray Elements are:");
    for(i=0;i < 10;i++)
    {
        printf("%d\t",*(p+i));
    
    }
    //checking the occurrence of number 'n' in array.
    for(i=0; i < 10;i++)
    {
        if(*(p+i)==n)
        {
            count++;//counting the occurrence of number 'n'.
        }
    }
    //printing the occurrence of number 'n'
    printf("\nNumber %d has occur %d times in an array",n,count);
    }
    

    Output:
    Enter the number:10


    Enter the Element:15
    Enter the Element:20
    Enter the Element:10
    Enter the Element:10
    Enter the Element:10
    Enter the Element:31
    Enter the Element:29
    Enter the Element:30
    Enter the Element:25
    Enter the Element:21



    Array Elements are:15    20    10    10    10    31    29    30    25    21


    Number 10 has occur 3 times in an array.


  • This is how calloc() functions works.
  • This is how we can allocate the memory through calloc() functions.