I was just curious as to if this code would create multiple memory leaks, or if it would get cleaned up correctly.
Node *newNode;
for (int i = 0; i < 10; i++)
{
newNode = new Node();
}
delete newNode;
So obviously the code doesn't do anything, but it does help me explain my scenario. Am I allocating memory 10 times and when I'm deleting the pointer leaving 9 orphans? Or am I reusing the same space being allocated and removing the orphan correctly? Thanks in advance!
Yeah this is leaking memory. When you do:
newNode = new Node();
You are redefining the pointer to point to newly-allocated memory, in effect losing hold of a way by which to address the previously-pointed to memory in order to delete it.
So when you leave the loop, the newNode
pointer points to the last-allocated (tenth) memory/Node
. When you delete newNode
you are deleting only that memory. You no longer have a way by which to delete
the others.
As Zhi Wang pointed out, you can use some form of smart pointer (unique_ptr
or shared_ptr
in C++11 for example). These smart pointers are basically wrappers around regular pointers which have additional semantics which prevent this kind of leak. If you used one of these, the memory/objects would automatically be deallocated when they went out of scope (upon ending of the current iteration of the for
loop in that case).
However, I don't think this would solve your situation in this case. I doubt you want to delete
the 10 objects as soon as you create them. Rather, you probably want to store these objects in a container like a std::vector
or at the very least have an array of pointers pointing to each of these allocated instances. That way you will have the objects around (which I believe is what you want, since you're constructing them at all) and also have a way by which to remove them later.