[plt-scheme] Manipulating Scheme vectors with FFI

From: Benjamin Seppke (benjamin.seppke at gmail.com)
Date: Tue Oct 28 09:42:10 EDT 2008

Hi everybody!

I'm very familiar with Scheme, but relatively new to the FFI (foreign
function interface) of Dr/MzScheme. It seems to be more different from
the one implemented in CommonLisp  (Allegro CL) that I thought.

The use case is, that  I want to call vector manipulation functions
that are written in C++ and linked via a C - dynamic library.

At the C-side a typical signature ist:
LIBEXPORT int function1(const float *arr1, const float *arr2,const
int length, const  float param)

I have two constant pointers to non-constant data. This function does
something using the contents of array1  (of length "length") and saves
the result to array2 (of equal length).

So I wanted to do the following steps in Scheme:
1. Create both vectors (arr1, arr2)
2. send them to the function (via ffi)
3. read the contents of arr2 and the resulting int-value (0 means no
error occured during calculation in the C function)

And that's it on the Scheme side:
(require scheme/foreign)
(define lib_c (ffi-lib "somewhere.dylib"))
(define ggm
    (get-ffi-obj "fun_c" lib_c (_fun (arr1 arr2 l s) ::
(arr1 : (_vector i _float))
(arr2 : (_vector i _float))
(l : _int)
(s : _float) -> _int)
                 (lambda ()
                   (error 'c_lib "installed foolib does not provide

(define length 10000)
(define arr1 (make-vector length 2.0))
(define arr2 (make-vector length 1.0))
(define s 10.0)

(vector-set! arr1 5000 50.0)

(ggm arr1 arr2 length s)

The bindings work well, if I play with the parameters I get a failure
returned by the ggm function (resulting in a 1 delivered by the c-
function). However, the result is never visible to me, the second
vector arr2 still consists of the initial values 1.0 which should not

I already tried to use arr2: (_vector i _float) or (_vector io
_float)  in the scheme ffi parameter list, but this resulted in the
following error:
 _vector: bad syntax in: (_vector io _float)
the same for _cvector....

So my question is: How can I manipulate Scheme vectors inside a c-
function and see the results of that in Scheme..?

Thanks in advance,

Posted on the users mailing list.