Legal, but useless. If you think you've found the royal road to memory
management, consider the following:
char *ptr, &ptr2;
ptr=malloc(10);
ptr2=ptr+2;
free(ptr);
ptr=NULL;
if (ptr+2) crash_hard_and_without_grace();
I don't think there are any magical cures for undefined behavior
caused by programming errors. The program may also crash hard and
without grace at the free(ptr) function call (or worse, of course).
In general, I think that setting a freed pointer to null is a good
idea. The fact that it won't cure every sort of pointer illness
doesn't negate the small value received in recompense.
IMO-YMMV.
P.S.
I almost always set freed pointers to null myself (the exception being
in C++ because it is pointless to set a deleted class member pointer
to null in a destructor.)
It helps with a very small class of problems (but does have one bad
side effect -- double frees are less likely to be detected) but
overall I think it is a tiny win.