[cffi-devel] Accessing integer arrays Lisp->C
Hi everybody, I have a problem that I was not able to resolve with the manual. I have the following C function (which is a trimmed down version of a C++ wrapper): extern "C" { void tryit(const int *ints, unsigned count) { for(unsigned i=0; i<count; i++) { cerr << "index " << i << ": " << ints[i] << "\n"; } } } /* extern "C" */ Now I want to call it with an array that is allocated and initialized on the Lisp side: (defcfun ("tryit" try-it) :void (vars :pointer) (count :unsigned-int)) (defun test () (let* ((l (list 1 2 3)) (len (length l)) (i 0)) (with-foreign-object (idx :int len) (map nil (lambda (x) (setf (mem-ref idx :int i) x) (incf i)) l) (try-it idx len)))) I'm quite convinced that this should now print 1,2,3 to cerr for the corresponding indices. However, I get the following: index 0: 197121 index 1: 0 index 2: 6581024 Any insight on what I'm doing wrong is greatly appreciated. Best regards, Stephan
Hello, On Mon, Nov 21, 2011 at 9:01 PM, Stephan Frank <sfrank@cs.tu-berlin.de> wrote:
(with-foreign-object (idx :int len) (map nil (lambda (x) (setf (mem-ref idx :int i) x)
MEM-REF's offset argument is byte-sized. You want MEM-AREF here.
(incf i)) l) (try-it idx len))))
BTW, cffi-devel requires subscription, I happened to notice your mail in the moderation queue, but usually there's too much spam and I just delete everything. Please subscribe. :-) Cheers, -- Luís Oliveira http://r42.eu/~luis/
Hello,
On Mon, Nov 21, 2011 at 9:01 PM, Stephan Frank <sfrank@cs.tu-berlin.de> wrote:
(with-foreign-object (idx :int len) (map nil (lambda (x) (setf (mem-ref idx :int i) x)
MEM-REF's offset argument is byte-sized. You want MEM-AREF here.
An embarrassing oversight on my part. Thank you very much.
(incf i)) l) (try-it idx len))))
BTW, cffi-devel requires subscription, I happened to notice your mail in the moderation queue, but usually there's too much spam and I just delete everything. Please subscribe. :-)
Yes, I figured that much when my mail didn't make it through and already have subscribed in the meantime. Thanks again. Regards, Stephan
participants (3)
-
Luís Oliveira
-
sfrank@cs.tu-berlin.de
-
Stephan Frank