Proper way to allocate an OCaml string from C code in OCaml 4.10?

Previously to allocate a string with explicit length (ie. one which may contain \0 characters) in C code we have used:

strv = caml_alloc_string (count);                                             
memcpy (String_val (strv), str, count);                                       

In OCaml 4.10 this doesn’t compile because String_val returns a ‘const char *’.

I could change String_val to Bytes_val, but that feels wrong. The runtime seems to use ‘&Byte_u (strv, 0)’.

It’s a shame there’s not a caml_copy_string_len function, but what is the proper way to do this for OCaml 4.10+, especially a way that won’t break in future and will be compatible with multicore?

You can use caml_alloc_initialized_string:

CAMLextern value caml_alloc_initialized_string (mlsize_t len, const char *);

How did I not see that. Perfect answer, thanks.