Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers in Lisp?

I've started learning Lisp recently and wanted to write a program which uses gtk interface. I've installed lambda-gtk bindings (on CMUCL). I want to have putpixel/getpixel ability on a pixbuf. But I found that I'm unable to direct access memory. (or just don't know how)

Function (gdk:pixbuf-get-pixels pixbuf) returns me a number - memory addr, I guess. In C++ I can easily get to the pixel I need. Is there any way to write my own putpixel in Lisp?

like image 258
x13n Avatar asked Nov 16 '09 16:11

x13n


People also ask

Are there pointers in Lisp?

Several functions for manipulating a fill pointer are provided in Common Lisp to make it easy to incrementally fill in the contents of a vector and, more generally, to allow efficient varying of the length of a vector.

What is an object in Lisp?

A Lisp object is a collection of consecutive memory cells. Each cell is large enough to hold the address of any Lisp object; this stored address is called a pointer. For example, a cons consists of two memory cells, one for the car and one for the cdr.


1 Answers

In Lisp, modern and portable way to access C libraries and to do direct memory access is CFFI.

You can use it like this:

>(defparameter *p* (cffi:foreign-alloc :unsigned-char :count 10))
;; allocate 10 bytes
*P*
> (setf (cffi:mem-aref *p* :unsigned-char 0) 10)
;; access *p* as an array of bytes and set its 0th element to 10
10
> (cffi:mem-aref *p* :unsigned-char 0)
;; access *p* as an array of bytes and take its 0th element
10
> (cffi:make-pointer 123)
;; make a pointer that points to given address
#.(SB-SYS:INT-SAP #X0000007B)
like image 97
dmitry_vk Avatar answered Sep 20 '22 21:09

dmitry_vk