Memory leak with pointer used without new operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mfactor

    #1

    Memory leak with pointer used without new operator

    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
    }
    }
  • joseph  cook

    #2
    Re: Memory leak with pointer used without new operator

    On Jul 12, 10:56 am, mfactor <ispma...@gmail .comwrote:
    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

    Comment

    Working...