pointer management within methods

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 ?

Nothing is freed automatically. For every new[], there must be a delete[].
Ofc you should delete them.
memory allocated on the heap has to be "freed" by the programmer.
Thanks for clearing that one for me !
Topic archived. No new replies allowed.