[Haskell-cafe] FFI and modifying haskell memory

Simon Marlow simonmar at microsoft.com
Fri Oct 21 10:17:27 EDT 2005

On 21 October 2005 14:40, Joel Reymont wrote:

> I stumbled upon this when binding the Zlib library and want to make
> sure I'm doing this right.
> foreign import ccall unsafe "uncompress" uncompress_ ::
>      Ptr Word8 -> Ptr CULong -> Ptr Word8 -> CULong -> IO CInt
> You give this function the address of your size as second argument
> and it updates size for you. basic uncompress(char* buf, unsigned
> long *size, ...) for you.
> Is it correct that I need to allocate a chunk of 4 bytes to be passed
> into C and updated there and then need to peek my value from that
> chunk of memory?

yes, something like

   with $ \pculong ->
     r <- uncompress_ buf pculong ...
     size <- peek pculong

> I thought StablePtr would give me the address of a Haskell value but
> it gives me an offset into some table instead.

That's right, the reason being that Haskell values move around when GC


More information about the Haskell-Cafe mailing list