M
Modular5mo ago
PavanMV

Working LinkedList using pointers

https://github.com/MVPavan/mojos/blob/master/learn/dsa/my_linked_list.mojo I have created linked list in Mojo, it runs fine with all the methods and deletions. However debug is failing, any idea why ? Also any feedback on deletion, memory leaks is also appreciated.
GitHub
mojos/learn/dsa/my_linked_list.mojo at master · MVPavan/mojos
Collection of mojo codes. Contribute to MVPavan/mojos development by creating an account on GitHub.
1 Reply
rd4com
rd4com5mo ago
Hello @PavanMV , the current __moveinit__ implementation does not free existing.data yet (remember, you move what is in some place, to another place, have to free the previous space)
#current implementation
fn __moveinit__(inout self, owned existing:Self):
self = Self()
self.data = self.data.alloc(1)
move_pointee(src=existing.data, dst=self.data)
self.next_ptr = existing.next_ptr
self.prev_ptr = existing.prev_ptr
#need to free existing.data
#current implementation
fn __moveinit__(inout self, owned existing:Self):
self = Self()
self.data = self.data.alloc(1)
move_pointee(src=existing.data, dst=self.data)
self.next_ptr = existing.next_ptr
self.prev_ptr = existing.prev_ptr
#need to free existing.data
It could be a good idea to sort of do it again (re put-together the parts), and each time before adding the next feature/part, make sure that the tests passes 👍 That way you'll catch and fix the 'bugs' as they come and it is easier to think.
Want results from more Discord servers?
Add your server