Kavya said:
Why is free(p) when p is a void * not undefined behavior?
What you get back from malloc() is a void*, so it makes sense that what
you pass to free() is a void*. C's implicit conversion rules between
void* and other pointer types also make this the best type when you
don't know the type of the pointer you'll be passing to (or returning
from) functions.
I am asking this because delete p when void* is most certainly
undefined behavior. Sorry for relating things to C++ but I think it
was
necessary to mention this.
The situation with void* is different in C++ because C++ (a) doesn't
have the same implicit conversion rules, and (b) allows you to overload
functions so that you can provide versions accepting a variety of
specific types transparently.
Regarding new and delete, think it through: You have to know what type
you're allocating for new to be able to find the right constructor, and
delete must know what type you're deallocating in order to find the
right destructor. C doesn't have con/destructors, so it doesn't need to
care what type the pointers are.
</OT>
S