DYNAMIC MEMORY ALLOCATION


Note : - In C language Dynamic Memory allocation is perform  in heap area of memory. 

* calloc( )

- It is prototype is specified within stdlib.h. This  function is used to allocate space in heap area of memory at runtime.
  In general calloc() is used to allocate structures, etc. The default value of allocated space which is allocated by calloc() is zero '0'.
  
  syntax :-  pointer-variable = (caste-type)calloc(array size, size of data type);
  Example :- int array[5]; {Static memory allocation}.
or
     int *array;
     array=(int*)calloc(5,sizeof(int));

Eg: Program for Array using calloc( )
- /* Program on array using calloc() also using free () function. */
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    void main()
    {
int size, *array, i;
printf ("Enter size of an array = ");
scanf ("%d",&size);
array=(int*)calloc(size,sizeof(int));
printf ("Default Value of Array :- \n");
for (i=0;i<size;i++)
{
    printf ("%d \t",*(array+i));  // it will print the default value / garbage value.
}
printf ("\n Enter %d elemnts :- \n",size);
for (i=0;i<size;i++)
{
    scanf ("%d",(array+i));
}
printf ("\n Elemnts of array are :- \n");
for (i=0;i<size;i++)
{
printf ("%d \t",*(array+i)); // it will free the array memory.
}
free(array);
        getch();
    }

  • Write a program to print the product of each prime number stored in Array memory must be allocated using calloc ( ) function.
   /* Program to print the products of eachh prime number stored in array using calloc() also using free () function. */
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    void main()
    {
       int size, *array, i, temp, count, j, product=1;
       printf ("Enter the size of an array = ");
       scanf ("%d",&size);
       array=(int *)calloc(size,sizeof(int));
       printf ("Enter %d element :- \n",size);
       for (i=0;i<size;i++)
       {
            scanf ("%d",array+i);
       }
       printf ("Enterd elemnts of %d are:- \n",size);
       for (i=0;i<size;i++)
       {
            printf ("%d \t",*(array+i));
       }
       for (i=0;i<size;i++)
       {
            temp = *(array+i);
            count=0;
            for (j=1;j<=temp;j++)
            {
                  if ( temp % j == 0)
          count++;
            }
            if (count == 2)
            product = product * temp;
       }
      printf ("\n");
      printf ("Product of all given prime number = %d",product);
       free(array);
    }

  • Write a program to perform linear search memory allocation for each variable memory must be allocated using calloc ( ) function.
   /* linear Search using dynamic memory allocation. */
   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   main()
   {
int size,*x, i, data, count=0;
printf ("Enter the size of an array = ");
scanf ("%d",&size);
x = (int*)calloc(size,sizeof(int));
printf ("Enter %d elemnts.:-\n",size);
for (i=0;i<size;i++)
{
    scanf ("%d",x+i);
}
printf ("Which number you want to search = ");
scanf ("%d",&data);
for (i=0;i<size;i++)
{
    if (data == x[i])
    count ++;
}
printf ("Total Number of %d is = %d\n",data,count);
for (i=0;i<size;i++)
{
    if (data==x[i])
    printf ("%d found at index number %d.\n",data,i+1);
    free(x);
}
   }

  • Write a program to print the sum of each element stored in array which has total four factor memory must be allocated using calloc ( ) function.
   /*Program to find the sum of each element which has four factor using dynamically.*/
   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   main()
   {
int size, *a, i, j, count, stored;
printf ("Enter the size of an array = ");
scanf ("%d",&size);
a=(int*)calloc(size,sizeof(int));
printf ("Enter %d elements :-\n",size);
for (i=0;i<size;i++)
{
    scanf ("%d",a+i);
}
for (i=0;i<size;i++)
{
count=0;
for(j=1;j<=*(a+i);j++)
{
    if(*(a+i)%j==0)
    count++;
}
if (count==4)
{
    stored+=*(a+i);
}
}
printf ("Sum of each element of four factor = %d",stored);
free(a);
   }

  • Write a program to print prime factor of each number stored in array memory must be allocated using calloc ( ) function.
   /* Prime factore using dynamic memory allocation.*/
   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   main()
   {
int size,*a,i,j,k,c;
printf ("Enter the size of an array = ");
scanf ("%d",&size);
a=(int*)calloc(size,sizeof(int));
printf("Enter %d elemts :-\n",size);
for (i=0;i<size;i++)
{
    scanf ("%d",a+i);
}
printf ("Prime factor in list :- \n");
for (i=0;i<size;i++)
{
    printf ("\nPrime factor of %d = ",*(a+i));
    for(j=1;j<=*(a+i);j++)
    {
   c=0;
   if (*(a+i)%j==0)
   for (k=1;k<=j;k++)
   {
if (j%k==0)
c++;
   }
   if (c==2)
   {
printf ("(%d) ",j);
   }
   }
}
free(a);
   }

  • Write a program to print all armstrong number stored in Array memory must be allocated using calloc ( ) function.
   /* Armstrong Number*/
   #include<stdio.h>
   #include<stdlib.h>
   #include<conio.h>
   main()
   {
int size, *a, store, rem, i, num;
printf ("Enter the size of an array = ");
scanf ("%d",&size);
a = (int*)calloc(size,sizeof(int));
printf ("Enter %d element :-\n",size);
for (i=0;i<size;i++)
{
    scanf ("%d",a+i);
}
printf ("Armstrong Number are :-\n");
for (i=0;i<size;i++)
{
    num=0;
    store = *(a+i);
    while (*(a+i)>0)
    {
rem = *(a+i)%10;
num +=rem*rem*rem;
*(a+i)=*(a+i)/10;
    }
    if (num == store)
            {
printf ("%d\n",store);
    }
}
free(a);
   }