0
Where to correct memory leaks?
I'm writing an improved iteration of a linked list, and, somewhat inevitably, have run into some memory leak detection. I've personally raked over my code a few times, making un-needed variables NULL before deleting them, but again, i still detect these memory leaks(detected using the CRT library in Visual Studio 2017) . Is there anywhere where i'm not correctly freeing up memory? as this question is up i'll try and review the code myself again. the actual code: https://code.sololearn.com/cMOemv3GkDBy
4 odpowiedzi
+ 4
Maybe you should review what new does?
In displayNodes for example you have this:
node *temp = new node;
temp = head; <-- memory leak right there
You allocate memory for temp and then make temp point somewhere else and it is lost, you never delete it, this happens all over your program.
+ 2
hey.
1.) don't use new, if you just want a dummy pointer! (example: line 71)
2.) use nullptr instead of NULL
3.) use a constructor for Node, to make sure next = nullptr.
4.) think about using smart pointer (auto_ptr, or unique_ptr, depends on the standard you use)
line 60, delete it, that should fix the error here.
after refactoring your code I would go over it again :)
0
vs inbuilt memory checking tool. that should do the job