Memory leak with pointer used without new operator

M

mfactor

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
}
}
 
J

joseph cook

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top