+ 1

Linked list question

What is difference between in this code with destructor amd without destructor. In code visualisation i didn't see any difference. https://code.sololearn.com/cMcb355dTeT1/?ref=app

25th Oct 2022, 7:37 PM
Abhay mishra
Abhay mishra - avatar
1 Answer
+ 3
Main role of destructor is release resources (usually memory) allocated by an object. A destructor is called automatically when an object is out of life if its been created on the stack (without new) or using delete with objects allocated on the heap like objects pointed by head, second end third in your code. In your code the destructor of a node deallocate the next one and so on then, if its not called, no dealloction would be. In your code no difference you will note (at end of program the os will "destroy" all anyway) BUT in more complex programs not deallocate memory is the source of a very common problems called memory leaks. Is really important know what the destructor is, how implement it in right way and when call it (like the constructor that i dont see in your code). P.S. I have tried to simplify the entire discourse about destructors but, in reality, them are more complex than that
26th Oct 2022, 5:31 AM
KrOW
KrOW - avatar