From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: vincent@parc.anglia.ac.uk (Vincent B.G. Leleu)
Cc: caml-list@inria.fr
Subject: Re: Interfacing C procedure using an array as parameter ?
Date: Tue, 29 Apr 1997 17:42:16 +0200 (MET DST) [thread overview]
Message-ID: <199704291542.RAA03587@pauillac.inria.fr> (raw)
In-Reply-To: <3364B837.4925@parc.anglia.ac.uk> from "Vincent B.G. Leleu" at "Apr 28, 97 03:46:15 pm"
> This is the first time I interface C/OCaml and also I have
> a problem : How do I use a parameter which is a pointer
> on the beginning of an integer array ?
To get you started, here are functions to convert between a C integer
array and a Caml integer array. (Warning: untested code ahead.)
value c_intarray_to_caml_intarray(int * carray, unsigned int nelts)
{
value mlarray;
int i;
mlarray = nelts < Max_young_wosize ? alloc(nelts, 0) : alloc_shr(nelts, 0);
for (i = 0; i < nelts; i++) Field(mlarray, i) = Val_int(carray[i]);
return mlarray;
}
int caml_intarray_to_c_intarray(value mlarray,
int ** carray /*out parameter*/,
unsigned int * nelts /*out parameter*/)
{
unsigned int len;
int * p;
int i;
len = Wosize_val(mlarray);
p = malloc(len * sizeof(int));
if (p == NULL) return -1;
for (i = 0; i < len; i++) p[i] = Int_val(Field(mlarray, i));
*carray = p;
*nelts = len;
return 0;
}
Hope this helps,
- Xavier Leroy
prev parent reply other threads:[~1997-04-29 17:44 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-04-28 14:46 Vincent B.G. Leleu
1997-04-29 15:42 ` Xavier Leroy [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=199704291542.RAA03587@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@inria.fr \
--cc=vincent@parc.anglia.ac.uk \
/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