but, wouldn't it be so much better if free() could check for NULL and simply ignore those cases?
instead the user will have to to do the check now and it wouldn't help the readability if there are such checks all over the program.
if (ptr1 != NULL)
free(ptr1);
if (ptr2 != NULL)
free(ptr2);
...
and so on.
is there any particular reason for ansi not having this feature?
No, it won't. Freeing a null pointer is defined behavior. It's defined as doing nothing.
and why????????????
Because the standard says so.
And hsn, format your posts properly. Poor capitalization, poorly formatted posts, excessive question marks, etc. don't sit well with people. This isn't IM chat. You have the time to properly write your posts. Do so, and you'll get more useful answers.
Just in case someone doesn't believe the previous poster, here is the text from
the final draft of the C99 Standard:
Originally posted by C Standard
7.20.3.2 The free function
Synopsis
[#1]
#include <stdlib.h>
void free(void *ptr);
Description
[#2] The free function causes the space pointed to by ptr to
be deallocated, that is, made available for further
allocation. If ptr is a null pointer, no action occurs.
Otherwise, if the argument does not match a pointer earlier
returned by the calloc, malloc, or realloc function, or if
the space has been deallocated by a call to free or realloc,
the behavior is undefined.
Comment