pointers manager ?

Discussion in 'C++' started by Marcelo Fernandez, Apr 20, 2007.

  1. 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
    Marcelo Fernandez, Apr 20, 2007
    #1
    1. Advertising

  2. Marcelo Fernandez

    anand chugh Guest

    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 <> 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
    anand chugh, Apr 20, 2007
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Apr 20, 2007
    #3
  4. 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
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Apr 20, 2007
    #4
  5. Marcelo Fernandez

    Zeppe Guest

    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
    Zeppe, Apr 20, 2007
    #5
  6. 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
    Roland Pibinger, Apr 20, 2007
    #6
  7. 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.
    >
    Gianni Mariani, Apr 20, 2007
    #7
  8. Thank you guys

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

    Marcelo
    Marcelo Fernandez, Apr 20, 2007
    #8
  9. Marcelo Fernandez

    James Kanze Guest

    On Apr 20, 5:54 pm, 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...).


    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:
    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, Apr 20, 2007
    #9
  10. Marcelo Fernandez

    James Kanze Guest

    On Apr 20, 6:05 pm, anand chugh <> 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:
    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, Apr 20, 2007
    #10
  11. Marcelo Fernandez

    James Kanze Guest

    On Apr 20, 6:07 pm, "Victor Bazarov" <> wrote:
    > 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.


    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:
    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, Apr 20, 2007
    #11
  12. On Fri, 20 Apr 2007 14:11:10 -0700, Marcelo Fernandez wrote:
    >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
    Roland Pibinger, Apr 21, 2007
    #12
  13. Marcelo Fernandez

    SimpleCode Guest

    On 4ÔÂ20ÈÕ, ÏÂÎç11ʱ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...).
    >
    > 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.
    SimpleCode, Apr 21, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    644
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    760
    Ron Natalie
    Sep 18, 2003
  3. padma
    Replies:
    0
    Views:
    347
    padma
    Oct 3, 2007
  4. Metalone
    Replies:
    0
    Views:
    344
    Metalone
    Jan 6, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
Loading...

Share This Page