pointer management within methods

Nov 18, 2012 at 7:24pm
I have a question that has been in the back of my mind for a while now and up to now I have assumed that the objects are destroyed after the function call but this is the question I have.

If I have a method that returns a pointer such as

WORD* myclass::Dosomething()
{
WORD* MyWrdPtr = new WORD [4];

WORD* MyOtherWrdPtr = new WORD [4];

WORD* w_res = new WORD [8];

// Do something and then

return w_res;

}

Do I need to delete the other two WORD pointers or are they automatically
destroyed ?






Nov 18, 2012 at 7:26pm
Nothing is freed automatically. For every new[], there must be a delete[].
Nov 18, 2012 at 7:27pm
Ofc you should delete them.
memory allocated on the heap has to be "freed" by the programmer.
Nov 18, 2012 at 8:17pm
Thanks for clearing that one for me !
Topic archived. No new replies allowed.