yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
368 stars 95 forks source link

Clarification about Ctypes.string #119

Open dbuenzli opened 10 years ago

dbuenzli commented 10 years ago

According to the docs on views. The string combinator is supposed to be a view on char *. This doesn't seem to be the case, is this a bug or not ? Related to #97

> cat bla_stub.c 
void test (char *s) 
{
  s[0] = 'A';
}

> cat bla.ml
open Foreign;;
open Ctypes;; 

let test = 
  foreign "test" (string @-> returning void)

let () =
  let s = "ab" in
  test s; 
  assert(s.[0] = 'A')

> ocamlfind ocamlopt -linkpkg -package "ctypes.foreign" -o bla.native bla.ml bla_stub.c
> ./bla.native 
Fatal error: exception Assert_failure("bla.ml", 11, 2)
Raised at file "string.ml", line 174, characters 25-34
Called from file "string.ml", line 197, characters 13-32
dbuenzli commented 10 years ago

Well I guess it's not a bug, if you want to use it with char * it's hard to give a clear semantics to it I think. But the docs should make it clear that you should only use that for const char * or a return type of char *.