How in the Racket FFI can you allocate an array of doubles, write numbers to it, pass it to a C function that modifies it, and read the updated array in Racket? I’ve been looking through the documentation but have not so far figured out how the various pieces are supposed to fit together. There are functions _array-ref
and _array-set!
, but how do you create such an array? I tried creating it with malloc
but that returns a cpointer
. How do you dereference the pointer to get an array that you can pass to _array-ref
?
I think this is the relevant part of the docs: “Instead, use ptr-ref with a pointer, an array type constructed with _array, and index 0 to convert a pointer to a Racket representation that works with array-ref and array-set!.”
@bkovitz you might also want to use f64vector
s
Thank, samth. Now testing…
That was one crucial piece of the puzzle, but I think I need one more. How do I pass a pointer to the array to the C function? (I haven’t looked at f64vector
yet.)
OK, trying it with f64vector
worked. This removes a tiny bit of type-checking, but that’s no problem. The interface here is very simple.