free -- Release Storage Blocks

Format

#include <stdlib.h>  /* also in <malloc.h>  */
void free(void *ptr);

Language Level: ANSI, POSIX, XPG4, Extension
free frees a block of storage. ptr points to a block previously reserved with a call to one of the memory allocation functions (such as calloc, umalloc,, realloc or _trealloc). The number of bytes freed is the number of bytes specified when you reserved (or reallocated, in the case of realloc) the block of storage. If ptr is NULL, free simply returns.

Note: Attempting to free a block of storage not allocated with a C memory management function or a block that was previously freed can affect the subsequent reserving of storage and lead to undefined results.

Return Value
There is no return value.

Example
This example uses calloc to allocate storage for x array elements and then calls free to free them.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
  long * array;    /* start of the array */
  long * index;    /* index variable     */
  int    i;        /* index variable     */
  int  num;        /* number of entries of the array */
  printf( "Enter the size of the array\n" );
  scanf( "%i", &num );
  /* allocate num entries */
  if ( (index = array = (long *)calloc( num, sizeof( long ))) != NULL )
  {
      /*  do something with the array  */
    for ( i = 0; i < num; ++i )     /* put values in array    */
       *index++ = i;                /* using pointer notation */
    free( array );                  /* deallocates array  */
  }
  else
  { /* Out of storage */
    perror( "Error: out of storage" );
    abort();
  }
  return 0;
  /******************************************************************
     The output should be:
     Allocating memory for 100 long integers.
  ******************************************************************/
}


Memory Management


_alloca -- Temporarily Reserve Storage Block
calloc -- Reserve and Initialize Storage
_debug_free -- Release Memory
_debug_tfree -- Release Tiled Memory
malloc -- Reserve Storage Block
realloc -- Change Reserved Storage Block
_tfree -- Free Tiled Storage Block
<malloc.h>
<stdlib.h>