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.