Archived

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

gimp

new CDog, delete CAnimal ???

Recommended Posts

Is this safe?
  
class CDog : public CAnimal {};

CDog* doggie = new CDog;

then

void DeleteAnimal(CAnimal* a)
{
   delete a;
}
  
(...blah, you know what I mean...) So, does this only deallocate the memory of an animal or does the OS store the original pointer against an allocation and remember how much was allocated to the doggie? Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
I think this is testable...

Make a CDog class that contains a ''int test()'' function, that returns 1.

Then, create a CDog, delete it as an CAnimal, and then try to do pointer->test();

If the program only destroys the CAnimal part... then the last part of the CDog should still be in memory (it wasn''t removed), and since the pointer still points to what was *supposed* to be the beginning of the CDog... the function might still work.

In any case, if you find that this thing *works*, you know that delete doesnt remove the whole CDog.

If it doesn''t work, well, either it does remove it, or I didn''t get my memory jink right

Share this post


Link to post
Share on other sites