我希望能够从C库中返回int数组给OCaml程序。为了做到这一点,我想将它们包装在Bigarray.Array1.t
中。下面是C存根的一个小演示:
/* nativeintarray.c */
#include <stdio.h>
#include <assert.h>
#include <caml/memory.h>
#include <caml/bigarray.h>
CAMLprim value make_array(value unit) {
CAMLparam1(unit);
int* a = calloc (1, sizeof(int));
a[0] = -1;
/* Create a C-style bigarray, yes it is a memory leak */
CAMLreturn(caml_ba_alloc_dims(CAML_BA_NATIVE_INT | CAML_BA_C_LAYOUT, 1, a, 1));
}
我的示例程序如下:
(* nativeintarray.ml *)
open Bigarray
(** Type for int arrays from/to C *)
type ivector = (nativeint, nativeint_elt, c_layout) Array1.t
external make_array : unit -> ivector = "make_array"
let () =
Printf.printf "%nd\n" (make_array ()).{0}
由于nativeint是有符号整数,我希望程序输出-1,但结果却是:
% ocamlbuild nativeintarray_c.o
Finished, 1 target (0 cached) in 00:00:00.
% ocamlbuild -package bigarray -lflags nativeintarray_c.o nativeintarray.native
Finished, 4 targets (0 cached) in 00:00:00.
% ./nativeintarray.native
4294967295
正如我所说的,我天真地期望 nativeint
与我本地 C 编译器中的 int
匹配,但显然并非如此。有人能解释一下吗?
平台: Ocaml 4.02.3,gcc 6.1.1,全部为 64 位。