From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p783rGuO011010 for ; Mon, 8 Aug 2011 05:53:16 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuIDAA5dP05V2gB4i2dsb2JhbABCmC+PGwEBAQoLCxslgUABAQQBfgsLRhQoiCECulsOhjgEmziINA X-IronPort-AV: E=Sophos;i="4.67,335,1309730400"; d="scan'208";a="115222220" Received: from emailfrontal1.citycable.ch ([85.218.0.120]) by mail1-smtp-roc.national.inria.fr with SMTP; 08 Aug 2011 05:53:11 +0200 X-Alinto-smtpauth-localdomain: Yes Received: from seldon (unknown [85.218.93.111]) (Authenticated sender: guillaume.yziquel@citycable.ch) by emailfrontal1.citycable.ch (Postfix) with ESMTPA id BF387E64107; Mon, 8 Aug 2011 05:53:04 +0200 (CEST) Received: from yziquel by seldon with local (Exim 4.72) (envelope-from ) id 1QqGu3-000807-KM; Mon, 08 Aug 2011 05:53:23 +0200 Date: Mon, 8 Aug 2011 05:53:23 +0200 From: Guillaume Yziquel To: caml-list@inria.fr Message-ID: <20110808035322.GI29083@localhost> References: <20110808131504.d9137220d4b4401cc3450e5a@mega-nerd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <20110808131504.d9137220d4b4401cc3450e5a@mega-nerd.com> User-Agent: Mutt/1.5.20 (2009-06-14) Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p783rGuO011010 Subject: Re: [Caml-list] Val_int vs caml_copy_nativeint Le Monday 08 Aug 2011 à 13:15:04 (+1000), Erik de Castro Lopo a écrit : > Hi all, > > I'm writing a C stub function to allow the calling of a C library > function from ocaml. The return from the stub is a tuple and I'm > doing this: > > /* Package up the result as a tuple. */ > v_response = caml_alloc_tuple (3) ; > > Store_field (v_response, 0, Val_int (width)) ; > Store_field (v_response, 1, Val_int (height)) ; > Store_field (v_response, 2, caml_copy_string (code)) ; > > CAMLreturn (v_response) ; > > The above works now, but didn't work when I was using > caml_copy_nativeint() instead of Val_int() and I'd like to know > why. I found it especially confusing because caml_copy_string() > worked and was obvioulsy the right thing to do. Did you use CAMLlocal on v_response? Why don't you use some intermediate variable to store the result of caml_copy_nativeint beforehand and then use Store_field (or even a Field () = assignment with caml_alloc_small this case)? Judging by the code of caml_copy_nativeint in ints.c: CAMLexport value caml_copy_nativeint(intnat i) { value res = caml_alloc_custom(&caml_nativeint_ops, sizeof(intnat), 0, 1); Nativeint_val(res) = i; return res; } and of Store_field in memory.h #define Store_field(block, offset, val) do{ \ mlsize_t caml__temp_offset = (offset); \ value caml__temp_val = (val); \ caml_modify (&Field ((block), caml__temp_offset), caml__temp_val); \ }while(0) I do not see why that wouldn't work if CAMLlocal is properly used on v_response. If it's not properly used, you may be invalidating the v_response pointer when evaluating caml_copy_nativeint in the line value caml__temp_val = (val); Other than that, I do not really see an issue with your code. What do you mean by "doesn't work" precisely? > Cheers, > Erik -- Guillaume Yziquel