From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 20E6BBC6B for ; Tue, 17 Apr 2007 07:12:37 +0200 (CEST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l3H5CYKx010229 for ; Tue, 17 Apr 2007 07:12:36 +0200 Received: from localhost (orion [130.54.16.5]) by kurims.kurims.kyoto-u.ac.jp (8.13.7/8.13.7) with ESMTP id l3H5CRit018388; Tue, 17 Apr 2007 14:12:27 +0900 (JST) Date: Tue, 17 Apr 2007 14:12:21 +0900 (JST) Message-Id: <20070417.141221.36667064.garrigue@math.nagoya-u.ac.jp> To: stefano.ballabeni@yahoo.fr Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Interfacing C and OCAML using the bigarray library From: Jacques Garrigue In-Reply-To: <219784.75442.qm@web23312.mail.ird.yahoo.com> References: <219784.75442.qm@web23312.mail.ird.yahoo.com> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 46245742.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; interfacing:01 ocaml:01 bigarray:01 ocaml:01 bigarray:01 arrays:01 malloc:01 sizeof:01 mlvalues:01 dims:01 malloc:01 sizeof:01 dims:01 alloc:01 char:01 From: Stefano Ballabeni > I'm trying to send a dynamically allocated C matrix to > OCAML. > > I've seen that the bigarray library seems to be what I > need. > > The problem is that I can't pass a _dynamically_ > allocated matrix to OCAML, the values don't arrive to > OCAML. The error is on the C-side: multi-dimensional arrays with bigarray have a flat representation (which is the usual way to do it in C.) So your allocation should be: unsigned char *my_c_array; my_c_array = malloc(sizeof (unsigner char) * 2 * 2); and assignments should be corrected too. Jacques Garrigue > A quick example : > test.c > ---- > #include > #include > > value make_c_matrix(void) > { > long dims[2]; > unsigned char **my_c_array; > > my_c_array = malloc(sizeof (unsigned char *) * 2); > my_c_array[0] = malloc(sizeof (unsigned char) * 2); > my_c_array[1] = malloc(sizeof (unsigned char) * 2); > > my_c_array[0][0] = 3; > my_c_array[0][1] = 4; > my_c_array[1][0] = 5; > my_c_array[1][1] = 6; > > dims[0] = 2; > dims[1] = 2; > > > return (alloc_bigarray(BIGARRAY_UINT8 | > BIGARRAY_C_LAYOUT, 2, my_c_array, dims)); > }