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

Dear all, Anyone has ever tried exporting Haskell list to C array ? I read in the FFI for MarshalAlloc, it is mentioned that Haskell lists are typically passed as C arrays to C functions. However, how can we do that ? I have a list of Int (i.e. [Int]) which I would like to pass as int[] in C. Please kindly advise how to proceed. Best Regards, David Lo

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
participants (2)
-
David Lo
-
Peter Simons