English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] NULL as a caml value
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-05-13 (07:21)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] NULL as a caml value
From: Lauri Alanko <la@iki.fi>

> The docs weren't very clear on this, so I just wanted to check: is
> (value)NULL a legal caml value? Checking from the sources, it seems that the
> GC does leave alone all pointers that don't point to within the caml-managed
> heap. However, the docs state that only "memory allocated by malloc" can be
> turned directly into an abstract caml value. So may I rely on NULL working?
> It would ease dealing with many C interfaces.

Yes, NULL is valid, as are all word-aligned addresses outside of the
Caml heap.
Of course it should be given an abstract type, since there is nothing
you can do with it inside Caml.

Note however that the statement about "memory allocated by malloc"
being safe is to be taken carefully, since people have already
reported strange bugs with the following scenario:

* allocate a data structure with malloc
* get a direct pointer to it in caml
* free the structure
* expand the caml heap, unfortunately including the free-ed malloc block
* your pointer is now seen as a pointer inside the caml-heap,
  and followed by the GC, directly into a segmentation fault...

This is a rather improbable scenario, but better know it in advance.
Better to limit direct pointers to structure that are not going to be
free-ed, and use boxed ones otherwise.


Jacques Garrigue
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners