hi,

On Tue, Apr 03, 2007 at 01:14:20PM +0200, foobar wrote:
> Hi,
> 
> i need to be able to return a list from a c-function in an extension of
> mine.
> More specifically i need to convert an array of char-pointers into a
> list of strings and
> return that list.
> 
> I looked into the docs and fount C_list which is responsible for
> constructing lists.
> The problem here is that the elements need to be C_alloc'ed, which is
> not the case
> in my extension.
> 
> Maybe i have to copy the strings inside the array into an array of
> C_alloc'ed
> objects and pass this array to C_list?
> If so how is that actually done correctly?

i'd rather avoid the use of those functions because they are fairly
tedious to use and afaik you cannot use them to return large amounts of
data because the garbage collector is not invoked while your code
runs and thus you are limited by the nursery size.

imho it's better to return the array as a c-pointer and construct
the list in a scheme wrapper function somehow like this:

(let ((c-fn (foreign-lambda ...))
      (get-string (foreign-lambda* c-string ((c-pointer a) (int i))
                                   "C_return(((char**)a)[i]);")))
  (let loop ((a (c-fn ...)) (i length-of-array) (l '()))
    (if (zero? i)
        l
        (loop a (- i 1) (cons (get-string a (- i 1)) l)))))

bye,
hans.

ps: is foobar your first or your last name?  :)


_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to