Advertisement

new / delete short question

Started by July 28, 2001 11:08 PM
1 comment, last by Diodor 23 years, 6 months ago

int * pi = new int[64];
void* pv = (void*)pi;
delete[] pv;
 
Does this code produce memory leaks ?
The memory of the array itself should be clean correctly, but if you do that with an object that allocated memory and deallocate it in the destructor, then you will leak, because the destructor called will be of the static type of the pointer.


  class Test;Test* pt = new Test[45];delete [] pt;  


this will call ~Test because the static type of pt is Test.

  Test* pt = new Test4[45];void* pv = (void*)pt;delete [] pv;  


destructor called will be ~void. Doh! Test destructor is not called, so if Test allocated memory that was being deallocated in the destructor, you will leak.
Advertisement
Thanks Fortunately, I had no destructors in my project, so I was safe

This topic is closed to new replies.

Advertisement