bkovitz
2018-12-6 21:19:51

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?


samth
2018-12-6 21:25:08

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!.”


samth
2018-12-6 21:27:35

@bkovitz you might also want to use f64vectors


bkovitz
2018-12-6 21:36:44

Thank, samth. Now testing…


bkovitz
2018-12-6 22:05:41

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.)


bkovitz
2018-12-6 22:14:13

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.