From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA17087 for caml-redistribution@pauillac.inria.fr; Mon, 7 Feb 2000 15:32:48 +0100 (MET) Resent-Message-Id: <200002071432.PAA17087@pauillac.inria.fr> Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA21642 for ; Mon, 7 Feb 2000 14:57:04 +0100 (MET) Received: from enst.enst.fr (enst.enst.fr [137.194.2.16]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id OAA15152 for ; Mon, 7 Feb 2000 14:56:53 +0100 (MET) Received: from Montchapet.ecole (root@cal-ppp21.ppp.enst.fr [137.194.3.21]) by enst.enst.fr (8.9.1a/8.9.1) with ESMTP id OAA00856 for ; Mon, 7 Feb 2000 14:56:52 +0100 (MET) Received: from localhost (localhost [[UNIX: localhost]]) by Montchapet.ecole (8.9.3/8.9.3) id OAA23042 for caml-list; Mon, 7 Feb 2000 14:51:05 GMT From: Michel Quercia Reply-To: quercia@cal.enst.fr To: caml-list@Montchapet.ecole Subject: Re: Extending a list on the OCAML side that is initially allocated on the C side Date: Mon, 7 Feb 2000 13:30:30 +0000 X-Mailer: KMail [version 1.0.17] Content-Type: text/plain References: <3898BCF1.B4FE1971@ibmoto.com> MIME-Version: 1.0 Message-Id: <00020714510512.09929@Montchapet> Content-Transfer-Encoding: 8bit Content-Transfer-Encoding: 8bit Resent-From: weis@pauillac.inria.fr Resent-Date: Mon, 7 Feb 2000 15:32:48 +0100 Resent-To: caml-redistribution@pauillac.inria.fr : I have an OCAML and C function as follows : : **************OCAML side***************** : let simple_list_test m = m @ [100] : let _ = : Callback.register "simple_list_test" simple_list_test; : *********************************************** Your function simple_list_test expects a list argument which is represented in memory as a linked collection of values. : list = (int *) calloc(10, sizeof(int)); : new_list = call_caml_simple_list_test(list); This cannot work as you give to "simple_list_test" a C array of ints, not a linked list as expected by the Ocaml compiler. Have a look at the reference manual, chapter "Interfacing C with Objective Caml", section 14.2 "The value type". A list cell is a 3 word object containing a header, the value of the cell, and a pointer to the next cell. Here is a way to build a legal 10 element list : void go() { value list, new_list; int i; #define nil Val_int(0) /* builds a list with the 1..10 numbers */ for(list = nil, i = 10; i > 0; i--) { Begin_roots1(list); new_list = alloc(2,0); Field(new_list,0) = Val_int(i); Field(new_list,1) = list; End_roots(); list = new_list; } /* now calls simple_list_test */ new_list = call_caml_simple_list_test(list); /* prints the last item */ for (list = new_list; Field(list,1) != nil; list = Field(list,1)); printf("%d\n", Int_val(Field(list,0))); fflush(stdout); } : My understanding ... : all I have to do is to cast the pointer to a caml 'value' type. Never cast unless you are absolutely sure the datatypes agree. -- Michel Quercia 9/11 rue du grand rabbin Haguenauer, 54000 Nancy http://pauillac.inria.fr/~quercia mailto:quercia@cal.enst.fr