From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA02697 for caml-redistribution; Thu, 15 May 1997 11:35:31 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA19100 for ; Wed, 14 May 1997 18:15:03 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.5/8.7.3) with ESMTP id SAA27643; Wed, 14 May 1997 18:14:53 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA19082; Wed, 14 May 1997 18:14:52 +0200 (MET DST) From: Xavier Leroy Message-Id: <199705141614.SAA19082@pauillac.inria.fr> Subject: Re: C strings in O'Caml In-Reply-To: <199705130659.IAA22102@anjou.ens-lyon.fr> from Jean-Christophe Filliatre at "May 13, 97 08:59:06 am" To: Jean-Christophe.Filliatre@ens-lyon.fr Date: Wed, 14 May 1997 18:14:52 +0200 (MET DST) Cc: caml-list@inria.fr MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis > I'm interfacing a C library in O'Caml and I faced the following > problem. I have a C string declared in the library, and I want to see > it as a Caml string. Is it possible to do it without copying it ? If you have control over the allocation of the string, then you can put a Caml string header in the word preceding the beginning of the string, and twiddle the last byte of the last word of the string as described in byterun/alloc.c. Then, you can just pass the char * as a Caml value and any Caml code should work on it quite happily. (The char * pointer must be word-aligned, by the way.) If the C string is allocated outside of your code (e.g. in a library), then you can't fully disguise it as a Caml string. You can still return the char * as a Caml value (provided it's word-aligned), but you can't use String.length neither any of the "safe" string functions (those that checks the bounds) on this string. You can still use the undocumented (and highly unsafe) String.unsafe_get, String.unsafe_set, String.unsafe_blit and String.unsafe_fill on that pseudo Caml string. Just be very, very careful with out-of-bounds accesses. - Xavier Leroy