Possible Duplicate:
Is it safe to delete a void pointer?
Say I have a new
allocation to a class called MyClass
and allocation is as simple as:
MyClass *myClassPtr = new MyClass();
And I store reference to the list of void*
where I simply say
myListOfPointers.add(static_cast<void*>(myClassPtr)); // this has to be void*
And later I release memory so instead of doing:
delete myClassPtr
I use:
delete MyListOfPointer.get(0)
(Say myClassPtr reference is at zero-index.) Also, please note that it has to be void*
since this list can store different types of pointers so I wouldn't know the type of pointer that I am deleting:
So I can't do any thing like:
delete static_cast<MyClass*>(MyListOfPointer.get(0))
Is this way going to release the correct memory size? (sizeof(MyClass)
)?
Note:
I am not looking for any answer pointing to smart pointers.
A
void
pointer has no type information. IfMyClass
has a destructor, it will not be called. The compiler needs to know what it is deleting so it can generate the appropriate code. If all of your pointers in the list are of the same type then you should be storing that type in the list, not asvoid
. If the pointers are different types but derive from a common base type, then give that base type a virtual constructor and store pointers of that type instead.Deleting through a
void*
results in undefined behavior, so you are guaranteed nothing.Emphasis mine.
So even though you said not to say it, the answer is to create some form of smart pointer. It would need to use type-erasure to hide the type externally (allowing the heterogeneous list), but internally keep track of the type it was given and how to delete it. Something much like
boost::any
.The correct answer to this question is of course 'no'
Edit: asked to provide more information, even though I already did in a comment to the question, the deletion of a void* is undefined, and this question is another way of asking this one: Is it safe to delete a void pointer? - see the answers in there for details.
It is not necessary to use a smart pointer, it's just smart.
That being said, there are many other possibilities; the only thing to do is to store the type information along the actual object.
And now: