Date: Tue, 24 Sep 1996 15:36:36 +0200 (MET DST)
From: Jerome Vouillon <vouillon@clipper.ens.fr>
Subject: Re: Need help: O'Caml C Interface
To: Frank Christoph <christo@nextsolution.co.jp>
In-Reply-To: <9609231509.AA03139@sparc3.nextsolution.co.jp>
Message-Id: <Pine.3.89.9609241403.A29179-0100000@nave>
> 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