[Haskell-cafe] [ANN] Haskell FFI Tutorial
Evan Laforge
qdunkan at gmail.com
Tue Nov 11 05:55:57 UTC 2014
On Mon, Nov 10, 2014 at 11:46 AM, Donn Cave <donn at avvanta.com> wrote:
> Could you explain this problem a little further? I looked in my code
> and found lots of stuff like `(#poke termios, c_iflag) a iflag', so was
> alarmed for a moment, but naturally I've declared iflag Word32, via the
> hsc #type macro. And I'd expect the targets of poking to be like that -
> full of CChar and Word32 etc. - foreign types of the appropriate size.
> The idea that there'd be anything else in there changes the perspective
> somewhat, and if that's common, indeed the tutorial should account for it.
>
> (Or maybe I completely misunderstand what's going on here!)
If you've ensured that iflag is a C type, then you've avoided the
problem. But if, for instance, iflag was a Char, not a CChar, and you
poke it into a 'char' struct field forgetting to convert to a CChar,
you'll get memory corruption. I don't know if it's a common mistake,
but I sure made it (very infrequently, but once is enough, in fact
once is even worse), and the compiler won't tell you if you did. When
I mentioned it on the list way back when no one responded, so maybe
other people don't fall into that trap.
More information about the Haskell-Cafe
mailing list