free void*-- why not undefined behavior?

K

Kavya

Why is free(p) when p is a void * not undefined behavior? 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.
 
W

Walter Roberson

Kavya said:
Why is free(p) when p is a void * not undefined behavior?

Ummm, because free() is *defined* as taking void* as its parameter ?
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.

delete is an object destructor, and in C++ if you try to delete void*
then it doesn't know what kind of destructor to use. It cannot
infer a destructor, either, since void* is convertable to every
pointer type and hence all the possible destructors are equally plausible.

free() is not an object destructor: it just returns the memory, the
size of which it has kept track of through some unspecified mechanism.
 
S

Stephen Sprunk

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
 
A

Andrew Poelstra

Why is free(p) when p is a void * not undefined behavior? 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.

In C, void* means "pointer to anything" as opposed to the intuitive
"pointer to nothing". So, as long as your void* has been previously
allocated by malloc(), it's perfectly legitimate to free() it.
 
D

Default User

Andrew said:
In C, void* means "pointer to anything" as opposed to the intuitive
"pointer to nothing".

Pointer to any object type. Function pointers need not be convertable
to void*.
So, as long as your void* has been previously
allocated by malloc(), it's perfectly legitimate to free() it.


The OP mentioned delete from C++. The type is important there, because
it does something different. There, if the original allocation was of a
type that makes it possible, the destructors for the objects are called
as well as freeing the memory. Obviously a void* wouldn't allow the
system to determine what if any destructors should be called.

The *alloc() family only deals with raw memory (although with copying
contents for relloc()), so the system only needs to know where it is.
For that, a void* works just fine.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top