The code I suggested in answer to your original question (create void pointer with
new
and then
delete
it) compiles and runs without error (although if you're so hard-pressed for memory that you need to make your pointers on the heap, you've got bigger problems).
1 2 3 4 5 6
|
int main()
{
void** p = new(void*); // Create a void pointer using new
delete p; // delete it
return 0;
}
|
Your code is an attempt to use
delete
on an object that was not allocated using
new
. When you allocate something using
new
, you get back a pointer to that object. If you then take that pointer and make it point to something else completely, as in your code, that doesn't magically mean you can use
delete
on what it now points to.
You code, as it stands now, applies delete to objects that were not allocated using
new
. This is wrong. Do not be fooled by the fact that your code compiles. The compiler has no option but to trust that you know what you are doing.
What exactly are you trying to do? I'm not convinced that you understand what
delete
is for.