Jump to content
  • Advertisement

Archived

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

Diodor

new / delete short question

This topic is 6201 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!