Explain briefly how to fix the memory leak in the insert implementation below. A
ID: 3596963 • Letter: E
Question
Explain briefly how to fix the memory leak in the insert implementation below. Assume that the destructor is implemented correctly. Note that there is no bug in insert other than the memory leak. Numbers on the left give line numbers.
Explanation / Answer
In the else statement at line 28 you are saying that the value is found and no need to insert. But you have created a BST node for that at line 3 and not used. So you have delete that node pointer.
So delete the created pointer if item is already there in the BST.
That means delete the created pointer in the else block at line 28. Other than that there are no other memory leaks.
So your else block at line 28 looks as follows :
---------------------------------------------------------------
else {
delete newNode; /* newNode created at line 3 */
}
--------------------------------------------------------------
In this way memory leak is solved.
/* hope this helps */
/* if any queries please comment */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.