free memory in C

C standard library offers the free() function, which takes a pointer as an argument and frees the memory the pointer refers to. This allows your system to reuse the memory for other software applications or other malloc() function calls

#include <stdio.h>
#include <stdlib.h>
 char *name;
 name = (char *) malloc(80*sizeof(char));
 if ( name != NULL ) {
 printf("\nEnter your name: ");
 printf("\nHi %s\n", name);
 free(name); //free memory resources
 } // end if

Memory allocated by malloc() will continue to exist until program termination or until a programmer “frees” it with the free() function. To release a block of memory with the free() function the memory must have been previously allocated (returned) by malloc() or calloc()

Leave a Comment