Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   pointers manager ? (http://www.velocityreviews.com/forums/t501727-pointers-manager.html)

Marcelo Fernandez 04-20-2007 03:54 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

anand chugh 04-20-2007 04:05 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.com> 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...).
>
> 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




Victor Bazarov 04-20-2007 04:07 PM

Re: pointers manager ?
 
Marcelo Fernandez wrote:
> 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



=?ISO-8859-1?Q?Erik_Wikstr=F6m?= 04-20-2007 04:14 PM

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

Zeppe 04-20-2007 05:08 PM

Re: pointers manager ?
 
Marcelo Fernandez wrote:

> 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

Roland Pibinger 04-20-2007 06:51 PM

Re: pointers manager ?
 
On Fri, 20 Apr 2007 08:54:08 -0700, Marcelo Fernandez wrote:
>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


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch

Gianni Mariani 04-20-2007 08:27 PM

Re: pointers manager ?
 
Erik Wikström wrote:
> 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.


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

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


Marcelo Fernandez 04-20-2007 09:11 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

James Kanze 04-20-2007 10:31 PM

Re: pointers manager ?
 
On Apr 20, 5:54 pm, Marcelo Fernandez <marche_1...@hotmail.com> wrote:

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

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


James Kanze 04-20-2007 10:37 PM

Re: pointers manager ?
 
On Apr 20, 6:05 pm, anand chugh <anand.ch...@gmail.com> wrote:
> 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.

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



All times are GMT. The time now is 02:52 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.