Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Using C values in ocaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-01-31 (15:23)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] Using C values in ocaml code
On Thu, 31 Jan 2002, Tomasz Zielonka wrote:
> On Thu, Jan 31, 2002 at 12:12:51PM +0100, Markus Mottl wrote:
> > You can't just cast 'x' to an OCaml-value: if it is an integer, you'll
> > have to use the "Val_int"-macro. Otherwise the integer might exceed the
> > range allowed by OCaml and could be interpreted as a pointer by the GC,
> > possibly resulting in a crash (integers need to be tagged).
> It's even more fun. Odd ints would be interpreted as ints with halved
> value, even ints as pointers.

Yes, but since you were using an abstract type in the "external"
declaration, you wouldn't have been able to add things within OCaml
anyway. As long as the GC hadn't gone mad and killed the process, you'd
have had to do computations within C-functions.

> Both - an integer or a pointer.  It is not always possible to check
> whether it is a pointer or an integer not knowing the context. 
> I will use abstract block then.

This would be the best choice if you can't know the type of the C-value,
if finalization is not needed and if handling of integers and pointers
should be generic. Usually, you'll want to specialize the handling of
integers and also unboxed floats whenever possible to improve performance
(no indirection to access values).

Markus Mottl

Markus Mottl                                   
Austrian Research Institute
for Artificial Intelligence        
Bug reports:  FAQ:
To unsubscribe, mail  Archives: