Jump to content
  • Advertisement
Sign in to follow this  
Welshy

Linked list destructor

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, creating myself a linked list here, and hoping to be able to make it generic and enable it to be used anywhere :) ANYWAY, i'm just looking at the list destructor. If the list contains nodes, with pointers to other nodes etc, do i need to delete all of the nodes separately to deallocate all of the memory correctly, or can i simply delete the list and the nodes should all go with it?

Share this post


Link to post
Share on other sites
Advertisement
Are you working in C++? If so, you need a delete for every new. If you're working in Java, it'll be GCed automatically when all the references to it are gone. If you're working in C -- again, a free() for every malloc().

P.S. -- I highly recommend creating your own linked list and using it somewhere, at least once. But once you have done that, move over to a standard list implementation; they're usually much more robust.

Share this post


Link to post
Share on other sites
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P

Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's

Anyway, cheers mate

Share this post


Link to post
Share on other sites
Quote:
Original post by Welshy
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P

Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's

Anyway, cheers mate


Awesome. That's how I all of a sudden came to understand memory managament in C++ -- creating a linked list. I like you, so I'd give you the delete algorithm, but you'll learn more implementing it yourself.

Ciao,
Twilight Dragon

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
Quote:
Original post by Welshy
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P

Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's

Anyway, cheers mate


Awesome. That's how I all of a sudden came to understand memory managament in C++ -- creating a linked list. I like you, so I'd give you the delete algorithm, but you'll learn more implementing it yourself.

Ciao,
Twilight Dragon

lol, thanks. Well i'll be at it all evening (i hope it doesnt take that long, though it probably will), i'll post it up once i've got it working and you can criticise it :P

Share this post


Link to post
Share on other sites
CList::~CList() // destructor
{
CNode *pPointer;

pPointer = pRoot; // sets the pointer to start at the beginning of the list

do
{
CNode *pPointerDel; // the delete pointer

pPointerDel = pPointer;
delete pPointerDel;

if(pPointer->pNext != 0)
{
pPointer = pPointer->pNext; // sets pointer to the next node in the list
}
} while(pPointer != pHead);

}

Here's what i've got, and it should work (i think).

But it doesnt run, so for pPointerDel to delete the node it's pointing to do i need to overload the assignment operator or something? i remember reading about it. Im thinking this could be the problem because when i set a breakpoint at the node destructor (which doesnt contain any code, yet) and it cannot resolve the values of the member variables, which im guessing means that the pointer doesnt contain all of the node information?

Share this post


Link to post
Share on other sites
Remember, if pointer a and pointer b point at the same thing, and either is deleted, both are then invalid [since the memory they point to is free'd/delete'd]

Share this post


Link to post
Share on other sites
Quote:
Original post by Welshy
Then how would you keep track of where you are in the deletion process?


That's part of the fun!

There's probably 3-4 ways to do it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!