[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jerome Vouillon <vouillon@c...> |
| Subject: | Re: Need help: O'Caml C Interface |
> value ocaml_getCharWidth(value code)
> {
> double x, y;
> Push_roots(r,1);
>
> r[0] = alloc_tuple(2);
> getCharWidth(Int_val(code),&x,&y);
> Store_double_val(Field(r[0],0),x);
> Store_double_val(Field(r[0],1),y);
> Pop_roots();
> return r[0];
> }
I would write it this way:
value ocaml_getCharWidth(value code)
{
double x, y;
value res;
Push_roots(r,2);
getCharWidth(Int_val(code),&x,&y);
r[0] = copy_double(x);
r[1] = copy_double(y);
res = alloc_tuple(2);
Field(res, 0) = r[0];
Field(res, 1) = r[1];
Pop_roots();
return res;
}
Store_double_val is used to store a double in an array of double values
(which is a special kind of array). In other cases, you have to use
copy_double to allocate a block containing the double.
So, your code failed because Field(r[0],0) was not a pointer to an array,
but an uninitialized pointer.
*_roots are used to keep a pointer to both double values. Indeed, these
values can be moved by copy_double or alloc_tuple. The tuple is allocated
last, so that it can be directly filled (without having to use function
modify).
Jerome