Name

free

Synopsis

Releases allocated memory

#include <stdlib.h>
voidfree( void *ptr );

After you have finished using a memory block that you allocated by calling malloc(), calloc() or realloc(), the free() function releases it to the system for recycling. The pointer argument must be the exact address furnished by the allocating function, otherwise the behavior is undefined. If the argument is a null pointer, free() does nothing. In any case, free() has no return value.

Example

char *ptr;

/* Obtain a block of 4096 bytes ... */
ptr =calloc(4096, sizeof(char));

if ( ptr == NULL )
  fprintf( stderr, "Insufficient memory.\n" ), abort();
else
{
/* ... use the memory block ... */
  strncpy( ptr, "Imagine this is a long string.\n", 4095 );
  fputs( stdout, ptr );
/* ... and release it. */
  free( ptr );
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.