Data Structure Algorithms with C Language
About Lesson

free()

this is used to release memory that is allocated dynamically using previous three functions.

Syntax:

void free(ptr);

 

where ptr – previously allocated memory

 

The memory allocated in heap will not be released automatically.  Programmer should take the responsibility of release that memory.

//Example

#include<stdio.h>

#include<stdlib.h>

 

int *input()

{

    int *ptr,i;

    ptr=(int*)malloc(5*sizeof(int));

    printf(“Enter any 5 numbers:”);

    for(i=0;i<5;i++)

    {

        scanf(“%d”,ptr+i);

    }

    return ptr;

}

 

int main()

{

    int i,sum=0;

    int *ptr=input();

 

    for(i=0;i<5;i++)

    {

        sum+=*(ptr+i);

 

    }

    printf(“Sum is: %d”,sum);

    free(ptr);

    ptr=NULL;

}

You cannot copy content of this page