[Haskell-cafe] FFI and a struct containing a string and allocating memory space

Brian Sammon haskell-cafe at brisammon.fastmail.fm
Wed Aug 9 02:19:45 UTC 2017


On Tue, 8 Aug 2017 20:03:16 +0200
Patrick Chilton <chpatrick at gmail.com> wrote:

> You could make your own `withEventInfo :: EventInfo -> (Ptr CEventInfo ->
> IO a) -> IO a` that calls withCString internally, and gives the passed
> function a pointer that is valid for the execution of the function. You

This seems a little more complicated than the malloc method, so I think I'd save it for if using the malloc method makes my program too slow.

Also, I was thinking of a solution similar to what you suggested, and it seems like it would require having the "poke" for the EventInfo Storable not actually put all the data in the C Struct (or not actually using "poke", but something lower-level, for the more-efficient marshalling system).  I envisioned having the EventInfo poke only poking the int fields, and the withCString that populates the string field living elsewhere.  It seems like this would work, but it seems not quite kosher in some way to have a poke that doesn't marshal the full set of fields.




More information about the Haskell-Cafe mailing list