Hi,
How do I call a function that expect char**?
I test it with
/** C Code */ #include <stdio.h>
void expect_charpp(char** buffs){ printf("I got %s and %s", buffs[0], buffs[1]); }
;; Lisp side (cffi:with-foreign-object (argv :pointer 2) (setf (cffi:mem-ref argv :pointer 0) (cffi:foreign-string-alloc "Hello")) (setf (cffi:mem-ref argv :pointer 1) (cffi:foreign-string-alloc "Hello")) (cffi:foreign-funcall "expect_charpp" :pointer argv))
And I got a memory fault error in SBCL. If i comment out the code in C to make it not accessing the data, it won't err. So it looks like the error is in how I allocate the memory.
Please advice.
Pisin Bootvong,
On Wed, 2005-12-28 at 00:59 +0700, JoeSB COE9 wrote:
;; Lisp side (cffi:with-foreign-object (argv :pointer 2) (setf (cffi:mem-ref argv :pointer 0) (cffi:foreign-string-alloc "Hello")) (setf (cffi:mem-ref argv :pointer 1) (cffi:foreign-string-alloc "Hello")) (cffi:foreign-funcall "expect_charpp" :pointer argv))
And I got a memory fault error in SBCL.
You want to use MEM-AREF here, not MEM-REF. The optional third argument to MEM-REF is a byte offset to add to the pointer, whereas the third argument to MEM-AREF is an array index.
I admit this is a little confusing---I wonder if MEM-AREF might be better given a more generic name and documented as the default accessor for foreign pointers. I don't think user code will need to use the more primitive features of MEM-REF directly very often...
James