-
-
Notifications
You must be signed in to change notification settings - Fork 756
list.push_back
inside for loop doesn't work, pushes nodes incorrectly
#5116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What you're doing there is take the address of the same SimpleNode literal on the stack every time through the loop each time and trying to push it. If you give it a new node each time, it works: for i in 1 ..= 5 {
sn := new_clone(SimpleNode {
value = i,
})
list.push_back(&ll, &sn.node)
} |
@Kelimion Thanks, i think i understand. However, i am using tracking allocator, and it shows memory leaked. What does that mean, and what to do? (p.s. i am very new to memory management and such) |
It means you used memory you didn't free. In your previous example, the memory lived on the stack and so was reused when the procedure exited. In this case however, we had to allocate memory for each node so that To free it you can iterate over the list backwards and free it. // And free
it2 := list.iterator_tail(ll, SimpleNode, "node")
for n in list.iterate_prev(&it2) {
free(n)
} Another thing you could do is allocate the nodes from a memory arena and free them all at the same time. Suffice it to say, I don't believe this to've been a bug. |
thanks, that was lot of learning. You are right, this is not a bug, I should have put it in discussion. |
To be fair, you didn't know it wasn't a bug at the time. |
Context
In the following code, the commented code works, however
push_back inside
for loop doesn't work.Some other points:
push_back
topush_front
inside loop, went into an infinite loopOperating System & Odin Version:
Expected Behavior
Expected output:
Current Behavior
Current output
Failure Information (for bugs)
ll.head
is getting updated, i maybe wrongSteps to Reproduce
Please check code above
Failure Logs
Please include any relevant log snippets or files here.
The text was updated successfully, but these errors were encountered: