I am learning ocaml during the past weeks, and now I would like to understand the use of ctypes creating by myself the binding (not using dune stanza at this moment). In particular, to use a C code to access specific information in binary files, but I am don’t know how to fill an OCaml array from a pointer (my level of C is also beginner)
To be specific:
In C, I will get first information about array size and type (I already implement these functions using ctypes).
/* called before several procedures to get nfvar integer and n, m values.*/ int data[n][m] retrieval = get_array_int(nfvar, &data) /* the array data will have now the information of the binary file. */ /* In general the shape can be different, but known in advance */
In the case of Ocaml, I would have something like:
open Ctypes open Foreign let oget_array_int = foreign "get_array_int" (int @-> ptr int @-> returning int)
Now, I am lost about how to allocate an OCaml array of an specific type (here is Int) with the given dimensions to be filled by C code. If I understand well, I would define, a function that is using the previous
oget_array_int. With other pointers (not arrays) of just an integer I was using
Ctypes.allocate Ctypes.int. Maybe something similar to:
let readarray nfvar = let arr = Bigarray.(Array2.(create int) c_layout n m) in oget_array_int nfvar arr
In the book Read World OCaml I can’t find an specific example using Bigarray to begin with. Could I also deal with arrays of more than 3 dimensions?