Subject: [clisp-list] Weird problem with FFI and :out arguments

From: Michael Goffioul <[email-masked]>
Date: Fri May 7 08:14:03 2004

Hi, 
 
I have a problem I don't understand. I defined those 2 C-functions: 
 
int myfun3( int *a, int len ) 
{ 
·   int i; 
·   for ( i=0; i<len; i++ ) 
·   ·   a[ i ] = i; 
·   return 0; 
} 
 
int myfun4( char *a, int len ) 
{ 
·   int i; 
·   for ( i=0; i<len; i++ ) 
·   ·   a[ i ] = ( char )'a'+i; 
·   return 0; 
} 
 
and I compiled them in a shared lib: libmyfun.so. In LISP, I then use 
the following calls: 
 
(def-call-out myfun3 
  (:library "libmyfun.so") 
  (:arguments (name (c-ptr (c-array-max int 256)) :out :alloca) 
    (len int)) 
  (:name "myfun3") 
  (:return-type int) 
  (:language :stdc)) 
 
(def-call-out myfun4 
  (:library "libmyfun.so") 
  (:arguments (name (c-ptr (c-array-max char 256)) :out :alloca) 
    (len int)) 
  (:name "myfun4") 
  (:return-type int) 
  (:language :stdc)) 
 
Finally I test those 2 functions with code like: 
 
(multiple-value-bind (success name) 
  (myfun3 16) (if (zerop success) name 'failure)) 
(multiple-value-bind (success name) 
  (myfun4 16) (if (zerop success) name 'failure)) 
 
The first call (with integer) returns an empty array, while the second 
call (with char) returns a correctly filled array. The only difference is 
the element type. Did I miss something??? Please, any comment is welcome, 
I'm currently lost. 
 
Thanks. 
Michael.

---

From: Sam Steingold <[email-masked]>
Date: Fri May 7 08:22:07 2004

> * Michael Goffioul <[email-masked]> [2004-05-07 15:13:09 +0000]:
>
> call (with char) returns a correctly filled array. The only difference
> is the element type. Did I miss something??? Please, any comment is
> welcome, I'm currently lost.

note that the first element of the integer array you create in myfun3 is 0.
IIUC, C-ARRAY-MAX is 0 terminated, so as soon as it sees a 0, it thinks
that the array is finished.

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x))))