Using ctypes to bind C functions returning pointers to get arrays

I’m learning myself, but I believe you will need to allocate memory for the second argument of the oget_array_int, and then you will need to copy that into an OCaml array. Don’t know if if that can be done directly.

let getArray nfvar n m = 
    let buff = allocate_n int ~count: n*m in
    let ret = oget_array_int nfvar buff in
   (* you probably want to check if ret is an error  *)
    Array.init nfvar (fun i -> !@(buff +@ i))

Something like this worked for me when trying to call proc_listpids in macos. Probably there are better alternatives that this !@(buff +@ i) pointer arithmetic syntax. This syntax makes me think I am missing something.

Also this doesn’t account for the two dimensions of the C array.

my 2 cents.

2 Likes