* Interfacing C and OCAML using the bigarray library
@ 2007-04-17 3:11 Stefano Ballabeni
2007-04-17 5:12 ` [Caml-list] " Jacques Garrigue
0 siblings, 1 reply; 2+ messages in thread
From: Stefano Ballabeni @ 2007-04-17 3:11 UTC (permalink / raw)
To: caml-list
Hi everybody,
First of all, sorry for my english, it's so bad, but
I'll try to be understandable.
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.
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));
}
test.ml
--
external take_matrix : unit -> (int,
Bigarray.int8_unsigned_elt, Bigarray.c_layout)
Bigarray.Array2.t = "make_c_matrix"
let _ =
let m = take_matrix () in
print_string "-- CAML --\n";
print_int m.{0, 0};
print_string " ";
print_int m.{0, 1};
print_newline ();
print_int m.{1, 0};
print_string " ";
print_int m.{1, 1};
print_newline ();
Then I compile :
gcc -W -Wall -ansi -pedantic -c test.c
ocamlc -custom bigarray.cma test.o test.ml
I run it :
./a.out
and it displays :
-- CAML --
208 208
6 8
As you can see these values are not exactly the one I
entered in the C code.
So, is that possible to use a dinamically allocated C
matrix in OCAML using bigarray ?
If it is, what do I do wrong ? I have been searching
for days to make this lib work fine...
If not, do you have a pretty solution ?
Thanks in advance.
--
Stefano Ballabeni
stefano.ballabeni@yahoo.fr
___________________________________________________________________________
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions !
Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses
http://fr.answers.yahoo.com
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Interfacing C and OCAML using the bigarray library
2007-04-17 3:11 Interfacing C and OCAML using the bigarray library Stefano Ballabeni
@ 2007-04-17 5:12 ` Jacques Garrigue
0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2007-04-17 5:12 UTC (permalink / raw)
To: stefano.ballabeni; +Cc: caml-list
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));
> }
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-04-17 5:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-17 3:11 Interfacing C and OCAML using the bigarray library Stefano Ballabeni
2007-04-17 5:12 ` [Caml-list] " Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox