In Erels helpful videos there is in above mentioned video at 7:14 there is a point I do not understand:
sub Pop returns the last item of a list.
As I understand, what happens is:
line 15: res is filled with a pointer to the last listitem
line 16: this item is removed from the list
line 17: now res is expected to point to nothing, but it does not.
Why does this code work?
17: No, it can never happen that you have a dangling pointer. The object will only be garbage collected when there are no references to it. As long as you can reach the object in some way, its memory will not be released.
17: No, it can never happen that you have a dangling pointer. The object will only be garbage collected when there are no references to it. As long as you can reach the object in some way, its memory will not be released.
We aren't really talking about pointers here. The object is removed from the list however it is still alive as the local 'res' variable holds a reference to that object.
The context is different. Pointer is a C / C++ term and it is a low level feature. The pointer is the actual memory address.
Reference (not C++ reference) is a bit more ambiguous and higher level concept. You hold a reference to an object. You don't know the memory address and you aren't responsible for managing the memory yourself. From the developer perspective the variable holds the object the only thing that you need to remember is that the object is never copied.