Subject: Re: C++ briar patch (Was: Object IDs are bad)
From: Erik Naggum <erik@naggum.no>
Date: 1997/05/29
Newsgroups: comp.lang.scheme,comp.lang.lisp,comp.lang.misc,comp.lang.functional,comp.lang.c++
Message-ID: <3073934556898504@naggum.no>


* Johann Hibschman
| I just want to inject a few examples to make sure I know what you guys
| are driving at.

your examples are good.

| I don't know Common Lisp, but a pointer-like action in Scheme would be:
| 
| (define a (vector 1 2 3 4 5))
| (define b a)
| (vector-set! b 2 10)
| a
| => #(1 2 10 4 5)
| 
| I suppose this is "copying an existing pointer", right?

right.  you can't take the address of an existing object to create a new
pointer.  an object and the pointer to it are inseperable.  in C, you can
view them as distinct.

| As someone else pointed out, in C you could make a pointer to the third
| item in an array, but you can't in Scheme, AFAIK.  Can you do this in
| Common Lisp?

in Lisp, you can't make a pointer into an array just like that.  in C you
can, since you can have an object and a pointer to it as distinct types.
Lisp vectors are frequently optimized for certain types, such that you can
store 32-bit integers in vector slots even though the rest of the type
system allows only 30-bit fixnums, or you can store floating-point values
directly without boxing.  also, a Lisp array knows its dimensions and their
sizes.  various other features are available, too.  it would be necessary
to keep track of such things in a "pointer" into an array.

Common Lisp does offer a way to "point" into an array, through arrays
displaced to other arrays.

(setq foo (make-array 10 :initial-contents '(0 1 2 3 4 5 6 7 8 9)))
=> #(0 1 2 3 4 5 6 7 8 9)
(setq bar (make-array 1 :displaced-to foo :displaced-index-offset 5))
=> #(5)
(setf (aref bar 0) nil)
=> nil
bar
=> #(nil)
foo
=> #(0 1 2 3 4 nil 6 7 8 9)

clearly, this is not the trivial thing it is in C, where vectors are just
blocks of contiguous memory and a pointer to a particular object can just
be incremented to point to the next few bytes of the memory.  I don't think
it would be recommended practice to use displaced arrays like a "pointer",
but one could use `adjust-array' to modify an existing "pointer" object.

| So in Scheme at least, it seems like the only big problem is vectors,
| since most other data structures are built up out of cons-cells which
| can be used like pointers.

actually, very few of the other data structures in a Lisp system are
list-like.  most are indeed vector-like, since it is much more efficient
for structures of fixed size to be represented as contiguous blocks of
memory with named slots with direct accessors than the pointer-chasing in
lists.  in fact, the cons cell itself is just such a vector-like structure.

#\Erik
-- 
a software manager is a person who, upon reports of the existence of
icebergs, launches a major operation to remove the tip, yet refuses to
listen to experts who say that when the tip is removed, some more of the
iceberg will float up.  such a manager was in charge on the Titanic.