Memory leak with pointer used without new operator

Discussion in 'C++' started by mfactor, Jul 12, 2008.

  1. mfactor

    mfactor Guest

    Hello group,

    I've created a linked list with a node class and a list class, and to
    search the list, I use a pointer like this:

    node * current = getFirst();

    where getFirst() return the first node on the list. It works fine. My
    doubt is about memory allocation: with this, I think I've just
    referenced the pointer current to the address of the pointer to the
    first node on the list. Do I need to call delete on the end of the
    method?


    class node {

    node * first;
    ....
    }

    node* node::getFirst() {
    return first;
    }

    somewhere in the code...

    void list::search() {
    node * current = getFirst();
    while(current != NULL) {
    //do stuff
    }
    }
     
    mfactor, Jul 12, 2008
    #1
    1. Advertising

  2. mfactor

    joseph cook Guest

    On Jul 12, 10:56 am, mfactor <> wrote:
    > Hello group,
    >
    > I've created a linked list with a node class and a list class, and to
    > search the list, I use a pointer like this:
    >
    > node * current = getFirst();
    >
    > where getFirst() return the first node on the list. It works fine. My
    > doubt is about memory allocation: with this, I think I've just
    > referenced the pointer current to the address of the pointer to the
    > first node on the list. Do I need to call delete on the end of the
    > method?


    No, you aren't allocating any memory dynamically here. You aren't
    taking any ownership of the memory just by having a pointer to it.
    Whatever code is adding these elements onto the list may have to
    handle releasing memory, but that all depends on how that code is
    written.

    Joe Cook
     
    joseph cook, Jul 12, 2008
    #2
    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. Replies:
    3
    Views:
    12,092
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    709
    Eric Sosman
    Mar 22, 2005
  3. Replies:
    2
    Views:
    280
  4. Mike
    Replies:
    17
    Views:
    577
    Cameron Laird
    Apr 5, 2007
  5. xmllmx
    Replies:
    6
    Views:
    421
    xmllmx
    Feb 3, 2010
Loading...

Share This Page