0

Issue to update links in double link list | Shared_ptr

Hi Tried to implement link list using shared pointer. I am trying to attach next and prev pointers, but there seems an issue. Could anyone please help me understand issue here? https://www.sololearn.com/en/compiler-playground/c944JPowPy80 https://sololearn.com/compiler-playground/c944JPowPy80/?ref=app

17th Oct 2024, 6:27 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Réponses
+ 2
the main problems are in the <mylist::addToList> function: - the while loop condition is incorrect: you're checking "while (!b)" but <b> never changes inside the loop, which could lead to an infinite loop or incorrect traversal. however, there's still a design consideration to note: you're using <std::weak_ptr> for both "next" and "prev" pointers ( it wouldn't work ), which means you don't have any shared ownership of the nodes except through the head pointer. this could potentially lead to nodes being deleted prematurely ( the reference count become zero, and the weak_ptr becomes nullptr ). if you want a more robust doubly-linked list, you might want to use <std::shared_ptr> for at least the "next" pointer to maintain proper ownership of the nodes throughout the list. this would make the list more stable and prevent premature deletion of nodes, while still avoiding circular reference problems by keeping the "prev" pointer as a <std::weak_ptr> : https://sololearn.com/compiler-playground/ckorttG5D4at/?ref=app in general using smart pointers is risky with low-level data structures. the problem comes up when a long list is being destructed, it is being destructed recursively. node destructor calls smart pointer's destructor, which calls node destructor. most likely the stack will grow in O(n). since a list can be longer than the maximum possible stack, the code may run out of stack space and crash. I would advise against using smart pointers when implementing low-level data structures.
18th Oct 2024, 11:41 AM
MO ELomari
MO ELomari - avatar
+ 1
Ketan Lalcheta Interesting idea, but maybe just as an academic exercise. I was trying to understand your code, so I did some searching. It's not really practicable. https://stackoverflow.com/questions/36673391/c-linked-list-using-smart-pointers But yes, your code still would be a good bug hunting excercise. No solution from me right now, though.
18th Oct 2024, 5:11 AM
Bob_Li
Bob_Li - avatar
0
I got the idea why it is not working 😕 Head is the only one which is holding reference. Rest all are weak pointers. So , head is kept in memory and all other nodes are getting out of scope.
18th Oct 2024, 5:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Just to make a point for this smart pointers. I also understand that it is not needed apart from learning purpose. Here is what I have for single link list using unique_ptr. In case of double linklist, it is a bit difficult https://sololearn.com/compiler-playground/c8zO2X1L6uv8/?ref=app
18th Oct 2024, 5:41 AM
Ketan Lalcheta
Ketan Lalcheta - avatar