free Function

Frees memory.

Include

<stdlib.h>

Prototype

void free(void *ptr);

Argument

ptr points to memory to be freed

Remarks

Frees memory previously allocated with calloc, malloc or realloc. If free is used on space that has already been deallocated (by a previous call to free or by realloc) or on space not allocated with calloc, malloc or realloc, the behavior is undefined. This function requires a heap.

Example

#include <stdio.h>  /* for printf, sizeof, */
                    /* NULL                */
#include <stdlib.h> /* for malloc, free    */

int main(void)
{
  long *i;

  if ((i = (long *)malloc(50 * sizeof(long))) ==
       NULL)
    printf("Cannot allocate memory\n");
  else
  {
    printf("Memory allocated\n");
    free(i);
    printf("Memory freed\n");
  }
}

Example Output

Memory allocated
Memory freed