DYNAMIC MEMORY ALLOCATION


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

* Free ( )
- Free function used to deallocate space from heap area of the memory.
  Syntax :- free(pointer-variable);
Memory Allocation for array using malloc ( ).
syntax :- pointer-variable=(caste-type*)malloc(size of array);
Example :- 
int array[5]; { Static memory allocation}
or
int *array;
array=(int*)malloc(5*(int));

Eg: Program for Array using malloc( ) and free( ) function .
- /* Program on array using malloc() 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); // this is for size of an array.
array=(int*)malloc(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));
}
free(array); // it will free the array memory.
        getch();
    }