w3resource

C free() function

C free() function - Free allocated memory

Syntax:

void free(void *ptr)

The free() function is used to free a block of storage. The ptr argument points to a block that is previously reserved with a call to the calloc(), malloc(), realloc().

Parameters:

Name Description Required /Optional
ptr Previously allocated memory block to be freed. Required

Return value from free()

  • There is no return value.

Example: free() function

The following example shows the usage of free() function.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(10);
   strcpy(str, "w3resource");
   printf("String = %s,  Address = %u\n", str, str);

   /* Deallocate allocated memory */
   free(str);
   return(0);
}

Output:

String = w3resource,  Address = 7738336

C Programming Code Editor:

Previous C Programming: C calloc()
Next C Programming: C malloc()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/stdlib/c-free.php