Dynamic memory allocation:-
Malloc:-
In c malloc is a function that can allocate a block of memory of certain size .The size is given as argument to the function returns the address of that memory.
Syntax:- int *p; p=(int *)malloc(2);
Here,p is the address of allocated memory, (int *) is the cast of malloc and 2 is size of memory allocation in bytes.
A simple malloc function is always casted with a pointer to a data type ,because it returns the address and data type as the allocation is done for that.
Calloc:- Calloc is improminent over malloc this function was design to allocate memory for multiple values .Calloc require two arguments the first is size of single block and second is number of blocks.
Syntax:-int *p; p=(int *)calloc(sizeof(int),6);
Here,p is the address of allocated memory, (int *) is the cast of calloc and, sizeof(int) is size of memory allocation in bytes,and 3 is numbr of blocks allocated.
Realloc:-Realloc was designed to extend the memory that was already allocated either by malloc or by calloc realloc also requires two arguments the first is pointer of previous allocation and second was the size upto
which the memory is to be extended.
Syntax:- int *p; p=(int *)calloc(p,2);
Here,p is the address of allocated memory, (int *) is the cast of realloc and ,p is pointer of previous allocation,and 20 is number of blocks allocated.
Free:-Free function was designed to release the pointer that was assigned an address.
Syntax:-free(p); here p is a pointer which is to be released.
Programs using dynamic memory allocation:--
Single memory allocation via malloc:-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
void main()
{
int *p;
clrscr();
p=(int *)malloc(2);
printf("enter an integer value");
scanf("%d",p);
printf("your value is %d",*p);
getch();
}

Multiple allocation via malloc:-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
void main()
{
int *p;
clrscr();
p=(int *)malloc(2);
for(i=0;i<6;i++)
{ printf("enter an integer value");
scanf("%d",(p+i));
}
for(i=0;i<6;i++)
{
printf("your value is %d",*(p+i));
}
getch();
}

Allocation via calloc:-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
void main()
{
int *p;
clrscr();
p=(int *)calloc(2,6);
for(i=0;i<6;i++)
{
printf("enter an integer value");
scanf("%d",(p+i));
}
for(i=0;i<6;i++)
{
printf("your value is %d",*(p+i));
}
getch();
}

Setting the pointer free:-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
void main()
{
int *p;
clrscr();
p=(int *)malloc(2);
for(i=0;i<6;i++)
{
printf("enter an integer value");
scanf("%d",(p+i));
}
for(i=0;i<6;i++)
{
printf("your value is %d",*(p+i));
}
free(p);
getch();
}

Program for reallocate:-

#include< stdio.h>
#include< conio.h>
#include< alloc.h>
void main()
{
int *p;
clrscr();
p=(int *)malloc(2);
for(i=0;i<6;i++)
{
printf("enter an integer value");
scanf("%d",(p+i));
}
for(i=0;i<6;i++)
{
printf("your value is %d",*(p+i));
}
p=(int *)realloc(p,20);
for(i=0;i<10;i++)
{
printf("enter an integer value");
scanf("%d",(p+i));
}
for(i=0;i<10;i++)
{
printf("your value is %d",*(p+i));
}
getch();
}