Export through FFI Haskell List to C array (by MarshalAlloc)

Peter Simons simons at cryp.to
Tue Nov 9 10:31:51 EST 2004


David Lo writes:

 > Anyone has ever tried exporting Haskell list to C array?

Check out the module Foreign.Marshal.Array, specifically the
functions:

  withArray    :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
  withArrayLen :: Storable a => [a] -> (Int -> Ptr a -> IO b) -> IO b

These will marshal a Haskell list into a C array and hand
the pointer to the given function. Because the array is gone
when your function returns, you don't need to worry about
freeing resources, catching exceptions, etc. If you need
more fine-grained control, you'll find the module also has
functions equivalent to the usual malloc/free way of doing
things.

Peter



More information about the Glasgow-haskell-users mailing list