From: Jacques Garrigue <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
Date: Tue, 17 Apr 2007 14:12:21 +0900 (JST) [thread overview]
Message-ID: <20070417.141221.36667064.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <219784.75442.qm@web23312.mail.ird.yahoo.com>
From: Stefano Ballabeni <stefano.ballabeni@yahoo.fr>
> 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 <caml/mlvalues.h>
> #include <caml/bigarray.h>
>
> 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));
> }
prev parent reply other threads:[~2007-04-17 5:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-17 3:11 Stefano Ballabeni
2007-04-17 5:12 ` Jacques Garrigue [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070417.141221.36667064.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
--cc=stefano.ballabeni@yahoo.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox