[Haskell-cafe] FFI and a struct containing a string and allocating memory space
Zemyla
zemyla at gmail.com
Fri Aug 11 05:20:12 UTC 2017
Then have a separate struct and datatype for the last three fields,
and make that Storable. Alternatively, look into using ForeignPtrs, so
you can have the finalizer free the memory for the EventInfo.
Another thing you might try instead, if you have control of the code,
is to change EventInfo to
struct EventInfo {
int year;
int month;
int day;
char event_name[1];
};
and use the variable-sized-struct-array trick whose actual name I don't know.
On Tue, Aug 8, 2017 at 9:19 PM, Brian Sammon
<haskell-cafe at brisammon.fastmail.fm> wrote:
> 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.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list