Unsafe aspects of ByteString

Iavor Diatchki iavor.diatchki at gmail.com
Sun Jan 28 19:50:41 EST 2007


Hello,

On 1/28/07, Duncan Coutts <duncan.coutts at worc.ox.ac.uk> wrote:
> We can put them in the IO monad. The same applies to packCStringLen and
> packMallocCString.

Adding packCString (and friends) to the IO monad does not make them
any safer (the IO monad hides some sins but not all ;-).  To see the
problem, imagine that packCString was of type "CString  -> IO
ByteString" (as Don suggested).  Then we still get the same weird
behavior if we replace:
let s = packCString x
with
s <- packCString x
in the example that I posted.

The problem is that 'x' allows us to mutate the memory area occupied
by s' but Haskell values are supposed to be immutable.  So if we want
to have these functions, then it seems that the best we can do is to
mark them with the "unsafe" label and be very careful how we use
them...

Out of curiosity (I have not looked at the implementation of
ByteString) is it true that ByteStrings created in this fashion
(i.e., with packCString) will not be garbage collected?

-Iavor


More information about the Libraries mailing list