[cffi-devel] creating two-dimensional arrays

hbabcockos1 at mac.com hbabcockos1 at mac.com
Wed Mar 15 06:06:04 UTC 2006


Hello,

What is the right way to create a 2D array to pass to a c-function  
that expects **arr?

I tried:

   (defun make-matrix (lisp-mat)
     "Creates a two-dimensional c array, initializes with lisp matrix"
     (let* ((x-dim (array-dimension lisp-mat 0))
	   (y-dim (array-dimension lisp-mat 1))
	   (c-mat (foreign-alloc :pointer :count y-dim)))
       (dotimes (y y-dim)
	(let ((cur (foreign-alloc :double :count x-dim)))
	  (setf (mem-aref c-mat :pointer y) cur)
	  (dotimes (x x-dim)
	    (setf (mem-aref cur :double y) (coerce (aref lisp-mat x y)  
'double-float)))))
       c-mat))

To pass the argument z to the c-function:

c_plmesh(PLFLT *x, PLFLT *y, PLFLT **z, PLINT nx, PLINT ny, PLINT opt);

(PLFLT is of type double & PLINT is of type int)

But some of the values appear to get mangled in transit.

Thanks,
-Hazen

(CFFI 0.9.0, SBCL 0.9.9, OS-X 10.4)




More information about the cffi-devel mailing list