Re: references and passing by value
- Posted by DerekParnell (admin) Apr 29, 2012
- 1025 views
let's say I have a linked list. now I want to remove one cell in the middle of this list,
so it would be joined with the next next cell.
since euphoria passes data by value, what will the behaviour look like in the low level parts?
will it copy "all the rest" in the list, or is it as fast as a reference change, pointer change?
It depends on how you have designed the linked list. If the entire list is contained in a single sequence, then each modification to it may involve copying data - removing elements from the front or back of the sequence will not cause data copying, and sometimes there is enough 'free space' in a sequence to avoid some data copying when adding elements.
Note that when a sequence is passed to a routine, even though the semantics is pass-by-value, at the low-level, only a pointer to the sequence is actually passed. And if the sequence is never modified by the routine, a copy of it is not taken.
Have a look at the std/eumem.e module. It emulates traditional RAM access with a malloc()/free() pair of functions. With this, one can build a linked list (or any other sort of linked structure) without having to force copies of sequences to be made. You can emulate pass-by-reference with this module.