smart pointers and simulations

I was studying Ira Pohl's predator-prey simulation online
http://www.iet.unipi.it/a.bechini/fond2docs/predator_CSD.pdf

and it seems to me that that a lot of memory leakage would occur whenever
the derived classes call the function "next".

1. Does deleting the pointers in the function dele() also delete the objects they are pointing to?
2. Am I correct in my assessment? If not, can someone explain to me why this won't be a problem.
3. Can auto_ptr be used to correct the problem, if any?
Last edited on
and it seems to me that that a lot of memory leakage would occur whenever
the derived classes call the function "next".

No. Although there is some memory that is not released explicitly in main.


1. Does deleting the pointers in the function dele() also delete the objects they are pointing to?
Feeding a pointer to delete causes the object referenced by the pointer to be destructed and the memory it occupied to be released. It does nothing to the pointer itself.


2. Am I correct in my assessment? If not, can someone explain to me why this won't be a problem.
next just returns a pointer to a new'd object. There is only a leak when we lose a pointer to an existing new'd object without deleting it.


3. Can auto_ptr be used to correct the problem, if any?
auto_ptr is deprecated. You should use std::unique_ptr instead. It would certainly correct the problem in main, although some would argue it isn't a problem at all.


Last edited on
thank you.
Topic archived. No new replies allowed.