pointers manager ?

M

Marcelo Fernandez

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...).

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...).

Thanks a lot !!

Marcelo
 
A

anand chugh

Hmmm....
Unfortunately C++ dont provide garbage collection...
U have to manually delete all your pointers ..whichever you have
allocated...

But there is good workaround for that...
Use Boost C++ library, you can use shared_ptr<>
Using that you d'nt have to worry for its deallocation...:)
 
V

Victor Bazarov

Marcelo said:
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...).

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...).

There are two approaches: either learn to manage their lifetime
or use a garbage collector. There are rules for managing lifetime
of dynamic objects, and I bet books talk about them (like ownership,
reference counting...), you just need to find the right book. As
for garbage collector, there are several implementations on the
market, just look for them.

There are no tools that can tell when the pointer "are suppose to be
dead". There are tools that can identify memory that has never been
deallocated after the program has finished running. Those are called
"memory leak detector". Look them up.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
Z

Zeppe

Marcelo said:
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...).


Just in addition to what the others said: there is such a possibility,
but, as a beginner, in my opinion you should rather learn the
differences between Java and C++ In Java every Object is actually a
pointer, while in C++ this is not true, and you should really try to
make clean code that doesn't use too many pointers.

When you'll have confidence with the new language, you will learn how to
use (and how NOT to use) smart_pointers and such things!

Regards,

Zeppe
 
R

Roland Pibinger

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...).

The tool is your understanding of genuine C++ idioms.
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...).

Look for RAII, eg. http://en.wikipedia.org/wiki/RAII
 
G

Gianni Mariani

Erik said:
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.

Electric fence only detects use of deleted memory, not memory leaks IIRC.
 
M

Marcelo Fernandez

Thank you guys

I will check for those tools, but I think I should really practice my
c++ programming. Thanks a lot!

Marcelo
 
J

James Kanze

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...).
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...).

Try the Boehm collector. It can be configured to detect such
things. (Of course, it can also be configured to be a full
garbage collector, which is recommended in new projects. It's
only when you have to deal with legacy code that you wouldn't
use it.)
 
J

James Kanze

Hmmm....
Unfortunately C++ dont provide garbage collection...

It's not required, but there are third party garbage collectors
which work quite well with it.
U have to manually delete all your pointers ..whichever you have
allocated...
But there is good workaround for that...
Use Boost C++ library, you can use shared_ptr<>
Using that you d'nt have to worry for its deallocation...:)

It's not a silver bullet. For that matter, neither is garbage
collection; even in Java, you have to consider object lifetime
issues. Still, garbage collection simplifies the implementation
considerably; boost::shared_ptr, while less general, can also
simplify it in certain cases.

However, while I would never forego garbage collection in
production code if I have a choice, for a student, I'm not sure
that it is such a good idea. The lack of garbage collection
means more coding, but its presence doesn't eliminate the
necessity of doing the design steps---it may take more work to
explicitly delete the pointers, but if you are not doing it
correctly, it's probable that you had object lifetime issues in
our Java code as well, and that there were, in fact, subtle bugs
in it as well.
 
J

James Kanze

There are two approaches: either learn to manage their lifetime
or use a garbage collector. There are rules for managing lifetime
of dynamic objects, and I bet books talk about them (like ownership,
reference counting...), you just need to find the right book. As
for garbage collector, there are several implementations on the
market, just look for them.
There are no tools that can tell when the pointer "are suppose to be
dead". There are tools that can identify memory that has never been
deallocated after the program has finished running. Those are called
"memory leak detector". Look them up.

Most such tools can also find dangling pointers---pointers to
memory that you have already freed. The two that I'm familiar
with are Purify (excellent, but definitly not priced for student
use), and valgrind (worked well the only time I used it, but
I've not enough experience with it to say more).
 
S

SimpleCode

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...).

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...).

Thanks a lot !!

Marcelo

You can use a excellent class called AutoFreeAlloc which is from WinX.
It has a perfect new and delete doing.
Also, It will autofree the memory you don't delete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top