Syntax
#include <stdlib.h> /* also in <malloc.h> */ void free(void *ptr);Description
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, 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.
There is no return value.
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 = 100; /* number of entries of the array */ printf("Allocating memory for %d long integers.\n", num); /* allocate num entries */ if ((index = array = calloc(num, sizeof(long))) != NULL) { /*.......................................................................*/ /*.......................................................................*/ /* do something with the array */ /*.......................................................................*/ 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. ****************************************************************************/ }Related Information