Re: pointers manager ?
On 2007-04-20 17:54, Marcelo Fernandez wrote:
Hi !
I was wondering if there is tool that could tell me if some of my
pointers are still alive when they are suppose to be dead (in case I
forget to kill them...).
There are tools for such things, I don't know exactly how good they are
since I only used one or two on fairly simple programs. What I do
remember was that they took quite some time to use since they basically
run the program through a debugger and for each allocation on the heap
it recorded the memory address and line of code that made the allocation.
One such tool is called Valgrind, electrical fence is another.
As you have noticed, I am real newbie in c++ programming and I have
quite a trouble to remember which pointer should be alive and which one
should die (Java garbage collector fault...).
A simpler way of doing things is usually to avoid using dynamic memory
unless necessary, and as you learn more C++ you'll see that it's not
necessary nearly as often as one might thing. In fact one can write
whole programs of quite some complexity without allocating memory on the
heap.
Since you have been using Java earlier you have gotten used to using new
whenever you create a variable, in C++ that is generally not the way to go.
Another useful feature of the C++ language is the auto_ptr and
shared_ptr (that one comes with boost I think) which work just like
pointers but they take care of the deallocation for you.
If you have a specific problem with some code you could post it here
(unless it's too large) and you can get some comments on it.
--
Erik Wikstr?m