Quote Originally Posted by rstanley View Post
free() is ONLY used if memory has been allocated using malloc(), calloc(), or realloc()! Or some other function that returns a pointer to memory it allocated. Always read the man pages for functions to understand the function completely!
AND... free works (do nothing) if the pointer is NULL (ISO 9899 7.20.3.2 § 2)... But if the pointer isn't NULL and not allocated by the functions above, then the behavior is unspecified (probably a segmentation fault or access violation).

This is OK:
Code:
void *p = NULL;

free( p );  // ok, do nothing
But this:
Code:
int main( void )
{
  int *p;

  free( p ); // probably will crash
}
Because `p` isn't initialized and can have ANY (probably invalid) value.