Memory Leaks?

Started by
3 comments, last by snk_kid 19 years, 7 months ago
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)
Advertisement
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.
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.
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 :)
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.

This topic is closed to new replies.

Advertisement