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

13th Dec 2017, 6:15 PM
X-1
X-1 - avatar
4 Answers
+ 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.
13th Dec 2017, 6:36 PM
Dennis
Dennis - avatar
+ 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 :)
13th Dec 2017, 6:39 PM
Gunther Strauss
Gunther Strauss - avatar
0
vs inbuilt memory checking tool. that should do the job
13th Dec 2017, 6:44 PM
Gunther Strauss
Gunther Strauss - avatar