[ocaml-ctypes] Pointer to pointer and null check

Andre Nathan andre at digirati.com.br
Fri Jul 22 12:32:04 BST 2016


Thanks a lot Jeremy!

I went with the ptr_opt way, thought I'm a bit surprised it works:

>   let foo_bar x =
>     let ret = allocate foo_opt (Some x) in
>     let r = foo_bar ret (Some x) in
>     (r, !@ret)

Intuitively I thought `ret` could never be null, because calling
`allocate` would be akin to calling malloc() + memset() in C. I also
believed that if the C library would set `*ret = NULL`, this would cause
a leak of the memory allocated in `allocate`.

So I guess my understanding is incorrect :)

In the actual code I used

  let ret = allocate foo_opt None in
  ...

since there's no meaningful initial value to be set on `ret`. It seems
to be working fine :)

Thanks again,
Andre


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20160722/050d001f/attachment.sig>


More information about the Ctypes mailing list