
April 20th, 2007, 04:55 PM
| | | pointers manager ?
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 | 
April 20th, 2007, 05:15 PM
| | | Re: pointers manager ?
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...:)
On Apr 20, 8:54 pm, Marcelo Fernandez <marche_1...@hotmail.comwrote: Quote:
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
| | 
April 20th, 2007, 05:15 PM
| | | Re: pointers manager ?
Marcelo Fernandez wrote: Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | 
April 20th, 2007, 05:15 PM
| | | Re: pointers manager ?
On 2007-04-20 17:54, Marcelo Fernandez wrote: Quote:
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. Quote:
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鰉 | 
April 20th, 2007, 06:15 PM
| | | Re: pointers manager ?
Marcelo Fernandez wrote: Quote:
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 | 
April 20th, 2007, 07:55 PM
| | | Re: pointers manager ?
On Fri, 20 Apr 2007 08:54:08 -0700, Marcelo Fernandez wrote: Quote:
>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. Quote:
>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
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch | 
April 20th, 2007, 09:35 PM
| | | Re: pointers manager ?
Erik Wikstr鰉 wrote: Quote:
On 2007-04-20 17:54, Marcelo Fernandez wrote: Quote:
>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.
| Electric fence only detects use of deleted memory, not memory leaks IIRC. Quote:
> Quote:
>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.
>
| | 
April 20th, 2007, 10:15 PM
| | | Re: pointers manager ?
Thank you guys
I will check for those tools, but I think I should really practice my
c++ programming. Thanks a lot!
Marcelo | 
April 20th, 2007, 11:35 PM
| | | Re: pointers manager ?
On Apr 20, 5:54 pm, Marcelo Fernandez <marche_1...@hotmail.comwrote: Quote:
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...).
| Quote:
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.)
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34 | 
April 20th, 2007, 11:45 PM
| | | Re: pointers manager ?
On Apr 20, 6:05 pm, anand chugh <anand.ch...@gmail.comwrote: Quote:
Hmmm....
Unfortunately C++ dont provide garbage collection...
| It's not required, but there are third party garbage collectors
which work quite well with it. Quote:
U have to manually delete all your pointers ..whichever you have
allocated...
| Quote:
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.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34 | 
April 20th, 2007, 11:45 PM
| | | Re: pointers manager ?
On Apr 20, 6:07 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote: Quote:
Marcelo Fernandez wrote: Quote:
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...).
| | Quote: Quote:
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...).
| | Quote:
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.
| Quote:
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).
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34 | 
April 21st, 2007, 05:45 PM
| | | Re: pointers manager ?
On Fri, 20 Apr 2007 14:11:10 -0700, Marcelo Fernandez wrote: Quote:
>I will check for those tools, but I think I should really practice my
>c++ programming. Thanks a lot!
| See also: Tom Cargill: Managing Dynamic Objects in C++ http://www.ddj.com/184409895
--
Roland Pibinger
"C++ is my favorite garbage collected language because
it produces so little garbage." - Bjarne Stroustrup | 
April 21st, 2007, 06:05 PM
| | | Re: pointers manager ?
On 4月20日, 下午11时54分, Marcelo Fernandez <marche_1...@hotmail.comwrote: Quote:
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. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|