Sign in to follow this  
onehundred

Memory Leaks?

Recommended Posts

Hey guys, i've been working with linked lists, understanding them and such... basically one author's method creates a pointer to a node on the free store and then when another node is created (linked etc) the tail node is then reassigned to point to this "new" node. All seems ok... except i've just read, when you reassign a pointer to a new object without deleting the old memory, it creates a memory leak? But if you delete the memory then the node is gone. is this ok to not delete it because I have linked it to the previous node or am i heading for a leak? (hope i've explained it ok)

Share this post


Link to post
Share on other sites
No, your implementation should be grand as long as you clean up once the list is destroyed. I think what you read just meant don't do this:


char* c = new char;
c = new char;


As you are leaking the memory assigned to c the first time.

Share this post


Link to post
Share on other sites
Not sure what you mean, if you allocate memory on the heap and you don't delete the memory you'll cause memory leaks, also if there are no more references to that memory you'll also cause memory leaks because you've lost the address to release the memory yourself.

Share this post


Link to post
Share on other sites
Thanks guys,

Quote:
Not sure what you mean, if you allocate memory on the heap and you don't delete the memory you'll cause memory leaks, also if there are no more references to that memory you'll also cause memory leaks because you've lost the address to release the memory yourself.


basically each new node of the linked list is created on the free store.

and yep i delete the nodes to clean up the list :)

Share this post


Link to post
Share on other sites
I know your doing this to learn, but once you've understood the concept, if your using c++ and generally any high-level language i recommend you learn to use the standard library containers & algorithms and use that in your programs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this