[Haskell-cafe] Double free-ing (was: Reading from a process)

Mitar mmitar at gmail.com
Thu Dec 17 23:57:27 EST 2009


On Fri, Dec 18, 2009 at 1:53 AM, Jason Dusek <jason.dusek at gmail.com> wrote:
>  You shouldn't have to use `malloc` and `free` to accumulate
>  input. Just append to a list or a ByteString, which is to say,
>  add a byte to a ByteString to get a new ByteString. Yes, really.

Oh, I did not know that ByteString has hGetNonBlocking. When I see
that System.IO's hGetBufNonBlocking uses Ptr I started crying and kept
crying while coding with it. It was really strange for me to use
Foreign.* modules for code which should be clean Haskell.

>  Maybe there is something I am missing here; but I can't see any
>  reason to adopt this unnatural (for Haskell) approach.

I checked ByteString's hGetNonBlocking now and I do see why it is
still better to use System.IO's hGetBufNonBlocking. I would like to
have a buffer of fixed length and just fill it until it is full. With
hGetBufNonBlocking this is possible as I can just give different
starting positions in the buffer while retaining data already read
where it is (so I do not to copy anything around). But with
hGetNonBlocking I would have to append two different buffers to get a
resulting buffer, what is completely unnecessary O(n). Why would I
read data into some other buffer just to be able to append it to main
buffer later.

Any suggestion how can I do that in Haskell why?


More information about the Haskell-Cafe mailing list