[cffi-devel] Accessing integer arrays Lisp->C

Stephan Frank sfrank at cs.tu-berlin.de
Mon Nov 21 21:01:49 UTC 2011


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




More information about the cffi-devel mailing list