free() function in C

Previous
Next

free() :

  • The memory which has allocated dynamically must be released explicitly using free function.
  • Function prototype is:
    • void free(void* ptr);
  • Using free() funtion , we can de-allocate the memory of any type of pointer.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () 
{
   char *str;

   str = (char*)malloc(15);
   strcpy(str, "tutipy");
   printf("String = %s,  Address = %u\n", str, str);

   str = (char*)realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);
   return(0);
}
Previous
Next

Add Comment

Courses Enquiry Form