free (NULL);

Noah Spurrier noah at wet.UUCP
Mon May 7 07:21:42 AEST 1990


Is there anything wrong with freeing a NULL? pointer? I have a function that
uses a static pointer. Everytime the function is called it frees up the
previous pointer using free, but the first time the function is called
there is nothing to free; the static pointer is initialized to NULL. This is
a good example of one of those annoying functions that work great as long
as they don't have a first time case.
Here is what I am doing...

char *squirl()
{
  static char *test = NULL;

  free (test);

  test = (char *) malloc (100);

  return (test);
}

This function is run many times so iI do not want to protect it with an if
because the if would only be useful for the first time it is run, after that
it just eats up run time.

   if (test != NULL)
      free (test);

Will angry things happen if I try to free(NULL) ?

yours,

Noah Spurrier
noah at WET.UUCP



More information about the Comp.lang.c mailing list