1

I am trying to figure out how software like SLIME or SLY gets the memory addresses of variables as it displays them in the Inspector. What Common Lisp function could I use to be able to do this programatically?

Example:

enter image description here

It is the #x100cab066d1 that is of interest here.

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • 1
    That's coming from the Lisp implementation itself. – Barmar May 30 '18 at 20:36
  • Try `(describe (list 1 2 3 4))` – Barmar May 30 '18 at 20:36
  • 1
    [`describe`](http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_describe.html) and [`inspect`](http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_inspect.html) – Barmar May 30 '18 at 20:38
  • I think `inspect` does it. Thanks! – MadPhysicist May 30 '18 at 20:42
  • 1
    Neither `inspect` nor `describe` have a specified format so the fact that you are seeing addresses is because the output is not standardized but implementation defined. That is not surprising since the CLHS should be able to be implemented from a language that don't expose addresses. I checked and did not get any addresses in CLISP and SBCL. – Sylwester May 30 '18 at 20:57
  • 2
    I think the `#<...>` is just what `PRINT-UNREADABLE-OBJECT` happens to output for the given object (eg. `(let ((list (list 1 2 3 4))) (print-unreadable-object (list *standard-output* :type t :identity t)))`). The memory address here is the identity part. There's no standard way to get it. SBCL seems to be getting it with `SB-KERNEL:GET-LISP-OBJ-ADDRESS`. – jkiiski May 30 '18 at 20:59

1 Answers1

6

You don't want to: garbage collection may (and often does!) move objects, so the address of an object may be different between observations. Another problem is posed by immediate objects (e.g., 1 or #\A) - what would their addresses be?!

That said, ANSI CL offers the :identity argument to print-unreadable-object which most lisps interpret to mean the current address in memory.

Alas, the output format is implementation-dependent (e.g., SBCL wraps the address in {}), so it is better to find the implementation-specific function which returns the address.

Using apropos, we easily find

  • system::address-of in CLISP;
  • sb-kernel:get-lisp-obj-address in SBCL.

PS. Check out sxhash - could that be what you are looking for?

sds
  • 58,617
  • 29
  • 161
  • 278