I am a little bit puzzled by the reference manual concerning the handling of OCaml strings in C stubs (§ 18.11.2) :
/* Copy the string argument to a C string, allocated outside the
OCaml heap. */
name = caml_stat_alloc(caml_string_length(vname) + 1);
name = caml_strdup(String_val(vname));
Can i simply use strdup like below ?
char * name = strdup(String_val(vname));
No because OCaml strings can contain NULL bytes, which would confuse
strdup. See the description of
String_val in the manual.
Thank’s for your response.
Does the function caml_strdup skip null characters so that the string can be interpreted correctly in C?
No. If you follow the definition you’ll see that it ends up with a memcpy. Recent compilers allow to check if the string is C safe with this function in