Liam Healy | 11 Jun 03:44 2008

Foreign struct

I'm having a problem defining/using a foreign struct for my package
GSLL (, an interface to the GNU
Scientific Library (, written in C.
I am trying to handle complex numbers; GSL has a struct defined to
represent complex numbers in /usr/include/gsl/gsl_complex.h:
typedef struct
    double dat[2];
So I define
(cffi:defcstruct complex-double-c
  (dat :double :count 2))
I set this value with a function
(defun complex-to-gsl-df (number)
  (let* ((gsl (cffi:foreign-alloc 'complex-double-c))
	  (cffi:foreign-slot-pointer gsl 'complex-double-c 'dat)))
    (setf (cffi:mem-aref datslot :double 0) (realpart number)
	  (cffi:mem-aref datslot :double 1) (imagpart number))
When I pass a complex number generated with this function to a GSL
function, I always get the following result: the real part is what I
specified for the imaginary part, and the imaginary part is the number
5.2635442471208903e-315, which I discovered is the single-float number
-1.0f0 interpreted as a double float (I don't know if that's
significant).  So for example the function #'test-set-all should set
every element of the complex vector to the same number 3+4i, but I get
(letm ((intvec (vector-complex-double-float 2)))
(Continue reading)

Luis Oliveira | 11 Jun 06:00 2008

New patches: 10-Jun-2008

Tue Jun 10 16:19:20 EDT 2008  Luis Oliveira <loliveira <at>>
  * Update make-shareable-byte-vector for LW 5.1

  Patch courtesy of Chun Tian.

    M ./src/cffi-lispworks.lisp -1 +4

An updated tarball of CFFI's source can be downloaded here:

Darcsweb URL:;a=summary
Luís Oliveira | 11 Jun 09:42 2008

Re: Foreign struct

On Wed, Jun 11, 2008 at 2:44 AM, Liam Healy <lnp <at>> wrote:
> There is a GSL function that sets the real and imaginary parts of the
> struct (in C of course).  When I use this, I get the answer I expect.
> Unfortunately, it's more restricted than setting the structure in CL,
> so I'd like to solve the structure problem.

I'm surprised that works. It seems that GSL's complex operators are
passed gsl_complex structs by value. That is not supported by CFFI.
Unless GSL provides another way to handle complex values, you'll
probably need to write C wrappers for each complex operator that take
two doubles and pass them as a struct. (Or a single wrapper that calls
a function pointer, I suppose.)

OTOH, CL already has all of these operators so perhaps you're doing
something else. Can you show me an example?


Luís Oliveira