C Language: Pointers 25

Example of malloc() function

Question:
C program to take input for “n” elements and display them allocate the memory dynamically.
Sol:

#include<stdio.h>
#include<stdlib.h>
int main()
{
  int n,i,*p;
  char ch;
  do
  {
   printf("Enter total elements ");
   scanf("%d",&n);
   p=(int*)malloc(n*sizeof(int));
   if(p==NULL)
   {
     printf("Memory allocation failed\n");
     exit(1);//header file : stdlib.h
   }
   for(i=0;i<n;i++)
   {
     printf("Enter the element ");
     scanf("%d",(p+i));
   }
   for(i=0;i<n;i++)
   {
     printf("%d\n",*(p+i));
   }
   free(p);
   printf("Like to cont ... (y/n) ");
   fflush(stdin);
   ch=getchar();//scanf("%c",&ch);
  }while(ch=='y' || ch=='Y');
  return(0);

}
/* Output */
Enter total elements 5
Enter the element 10
Enter the element 20
Enter the element 30
Enter the element 40
Enter the element 50
10
20
30
40
50
Like to cont ... (y/n) y
Enter total elements 3
Enter the element 2
Enter the element 6
Enter the element 3
2
6
3
Like to cont ... (y/n) n

Question:
C program to take input for “n” elements and display them also calculate and print sum of all the elements, allocate the memory dynamically.
Sol:

#include<stdio.h>
#include<stdlib.h>
int  main()
{
  int n,i,*p,s;
  char ch;
  do
  {
   printf("Enter total elements ");
   scanf("%d",&n);
   p=(int*)malloc(n*sizeof(int));
   if(p==NULL)
   {
     printf("Memory allocation failed\n");
     getch();
     exit(1);//header file : stdlib.h
   }
   for(i=0;i<n;i++)
   {
     printf("Enter the element ");
     scanf("%d",(p+i));
   }
   s=0;
   for(i=0;i<n;i++)
   {
     printf("%d\n",*(p+i));
     s=s+*(p+i);
   }
   free(p);
   printf("sum = %d\n",s);
   printf("Like to cont ... (y/n) ");
   fflush(stdin);
   ch=getchar();//scanf("%c",&ch);
  }while(ch=='y' || ch=='Y');
  return(0);

}
/* Output */
Enter total elements 5
Enter the element 10
Enter the element 20
Enter the element 30
Enter the element 40
Enter the element 50
10
20
30
40
50
sum = 150
Like to cont ... (y/n) y
Enter total elements 3
Enter the element 1
Enter the element 2
Enter the element 6
1
2
6
sum = 9
Like to cont ... (y/n) n

calloc()

The function of calloc() function is exactly the same as that of malloc() function. That is it helps us to allocate a block of memory dynamically.

Syntax:
datatype *pointervariable;
pointervariable=(cast type)calloc(n,s);
n: total number of memory locations
s: size of one element

To allocate memory dynamically for storing 10 integers

int *p;
P=(int*)calloc(10,sizeof(int));
P=(int*)calloc(10,2);

To allocate memory dynamically for storing 10 characters

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

In general to allocate memory dynamically for n integers

Before using the memory block which has been allocated dynamically it is very important to verify whether the memory has been successfully allocated or not. If memory is successfully allocated then base address of the allocated memory block is returned back and stored in the pointer variable. On the other hand if memory allocation fails then NULL is returned back and stored in the pointer variable.

int n,*p;
printf(“Enter total elements “);
scanf(“%d”,&n);
p=(int*)calloc(n,sizeof(int));
if (p==NULL)
{
printf (“memory allocation failed”);
return;
}

 

Difference between malloc() and calloc() function.

1.
While allocating memory using malloc() function we are required to specify one argument whereas while allocating memory with calloc() function we are required to specify two arguments.
p= (int*) malloc(n*sizeof(int));
P=(int*)calloc(n,sizeof(int));

2.
The memory allocated with the help of malloc() function by default contains garbage whereas the memory block allocated with the help of calloc() function by default initialized by zero.