Re: Passing primitive value by reference
On Monday 04 October 2004 01:20 pm, lisptracker wrote:
> As far as I know a primitive value being passed to a function, remains
> unmuttable object. So I have to use some wrapper... To answer a question I
> should find some way to do it like below:
Make sure that you are comparing apples with apples.
In C, you cannot do the following:
&7
or
&(3 + 4)
So, in C, you can't pass the address of a "primitive type", either!
In C, when you do this:
int x = 7;
...
&x
You are creating a wrapper (it's name is "x"), putting the primitive value 7
into it and then taking the address of the wrapper.
If you want to do *exactly* the same thing in lisp, you can use defstruct,
make-array, cons, defclass and probably a bunch of other things that don't
come to mind, to create the wrapper.
You can do *exactly* the same thing in Lispworks if you use the FLI, too.
You can play games with the :displaced-to option of make-array, to share
memory.
But, of course, you're using Lisp so that you never again have to deal with
the insanity of the baggage that C brings with it, right? :-)
You wouldn't solve the same problem the same way in Lisp as you do in C,
because Lisp gives you better ways of solving the problem - ways that are
much less error-prone (one of the points of using a better language!).
Taking the address of a variable with a cheap built-in syntax is something
you need to get rid of, so that you can have the much-more-important feature
of garbage collection.
Sharing memory - without using the address operator - in Lisp is easy. It
happens all of the time, for example in lists (e.g. if x is a list, then (cdr
x) shares memory with x). C++-people tend to build list operators with
*less* sharing and more wasted memory (e.g. cursors).
pt