Previous Page
Next Page

free

Releases allocated memory

#include <stdlib.h>
void free ( 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 );
}

See Also

malloc( ), calloc( ), realloc( )


Previous Page
Next Page