• Advertisement

Archived

This topic is now archived and is closed to further replies.

new / delete short question

This topic is 6055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int * pi = new int[64];
void* pv = (void*)pi;
delete[] pv;
 
Does this code produce memory leaks ?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Thanks Fortunately, I had no destructors in my project, so I was safe

Share this post


Link to post
Share on other sites

  • Advertisement