From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 3D285BC57 for ; Mon, 1 Mar 2010 14:26:02 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ar0DALlPi0vZSMDdimdsb2JhbACbDhUBAQEKCQwHEQUfiBuxEIR7BA X-IronPort-AV: E=Sophos;i="4.49,560,1262559600"; d="scan'208";a="57967115" Received: from fmmailgate01.web.de ([217.72.192.221]) by mail4-smtp-sop.national.inria.fr with ESMTP; 01 Mar 2010 14:25:49 +0100 Received: from smtp05.web.de (fmsmtp05.dlan.cinetic.de [172.20.4.166]) by fmmailgate01.web.de (Postfix) with ESMTP id 4E8CE14A04904 for ; Mon, 1 Mar 2010 14:24:46 +0100 (CET) Received: from [85.216.85.71] (helo=frosties.localdomain) by smtp05.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #314) id 1Nm5c6-0007Qo-00 for caml-list@yquem.inria.fr; Mon, 01 Mar 2010 14:24:46 +0100 Received: from mrvn by frosties.localdomain with local (Exim 4.71) (envelope-from ) id 1Nm5c5-0000Sw-OA for caml-list@yquem.inria.fr; Mon, 01 Mar 2010 14:24:45 +0100 From: Goswin von Brederlow To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] How to pass C pointers to Caml References: <9dfe358d1002281955k4cb9207bi4afe5d649fc20e05@mail.gmail.com> <201003010722.15601.monnier.florent@gmail.com> Date: Mon, 01 Mar 2010 14:24:45 +0100 In-Reply-To: <201003010722.15601.monnier.florent@gmail.com> (Florent Monnier's message of "Mon, 1 Mar 2010 07:22:15 +0100") Message-ID: <87tyt0jhsy.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX1/XECc7I2swFGDatneACwZsQsofNGK2fBtafqzl m3C+a5949vjqps5kwcBmktRl3xmcmMGAz9BdupwJ+NVcjGPZWi K6aUS80Lc= X-Spam: no; 0.00; pointers:01 ocaml:01 pointers:01 ocaml:01 pointer:01 pointer:01 trivial:01 allocates:01 zhao:98 boom:98 mfg:98 abstract:01 abstract:01 heap:01 heap:01 Florent Monnier writes: > Le lundi 1 mars 2010 04:55:00, Jianzhou Zhao a écrit : >> I have been calling OCaml code from C in my project. >> The C code has some pointers to C structures. >> I got 'seg fault' when calling the OCaml function receiving >> C structure pointers. >> >> 18.7 at http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html >> gives the examples that pass int into OCaml. These examples work for me. >> But, Does OCaml support to pass C structure pointers to OCaml? > > Yes it does. Just cast your pointer to the type value. > > In this tutorial there is an example "Pointers to C structures": > http://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.php#ref_ptr > > the pointer to a C struct is wrapped on the ocaml side by an abstract type > called "t" here, and it is provided back to C with print_t / dump_ptr. The problem with this trivial approach is that ocaml can store the pointer somewhere. When the C pointer is freeed then ocaml has a dangling pointer. Worse, if the GC allocates a new heap then the pointer might suddenly point into the heap and then BOOM. It is better to put the pointer into an abstract or custom block. MfG Goswin