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


new / delete short question

Recommended Posts

Gorg    248
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