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

Brian Sammon haskell-cafe at brisammon.fastmail.fm
Tue Aug 8 17:45:50 UTC 2017


If I have a struct like
struct EventInfo {
	char * event_name;
	int year;
	int month;
	int date;
}

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")

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.
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.

Thoughts?  Am I on the right track?  Am I missing any better approaches?

(I'm using Haskell 8.0.1 and a Storable instance for EventInfo, BTW)


More information about the Haskell-Cafe mailing list