<div dir="ltr">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 could also use <a href="http://hackage.haskell.org/package/inline-c">inline-c</a> and create a struct EventInfo when you need it, and then you know that your bindings are typesafe, which you don't get with Storable etc.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 8, 2017 at 7:45 PM, Brian Sammon <span dir="ltr"><<a href="mailto:haskell-cafe@brisammon.fastmail.fm" target="_blank">haskell-cafe@brisammon.fastmail.fm</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If I have a struct like<br>
struct EventInfo {<br>
        char * event_name;<br>
        int year;<br>
        int month;<br>
        int date;<br>
}<br>
<br>
and an associated haskell datatype that I want to marshall into it, it seems to me that I can't take advantage of the speed advantages of alloca (and its friends like "with")<br>
<br>
The only safe way I've thought of to do this is to do this is to have the poke function for EventInfo (the associated haskell type) call malloc (or newCAString or something) and then poking the string into the newly allocated space.<br>
I realized after a few successful runs of using withCString in the poke function for EventInfo, that I don't think that this (calling withCString from the poke function) was a safe approach, and I'm thinking it was just dumb luck that this worked at all.<br>
<br>
Thoughts?  Am I on the right track?  Am I missing any better approaches?<br>
<br>
(I'm using Haskell 8.0.1 and a Storable instance for EventInfo, BTW)<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br></div>