logo       

Re: peekCString stack overflow: msg#00008

lang.haskell.glasgow.bugs

Subject: Re: peekCString stack overflow

On Wed, Oct 01, 2003 at 05:34:47PM +0100, Alastair Reid wrote:
>
> > Yes, I've run into this before. In fact this is one of those tricky
> > problems where you can't quite get tail-recursion where you want it:
> > (pseudo-ish code follows)
> >
> > peekCString ptr = do
> > x <- peek ptr
> > if x == '\0' then return [] else do
> > xs <- peekCString (ptr + 1)
> > return (x:xs)
> >
> > Any ideas? I seem to recall the ML folks have a hack for dealing with
> > this.
>
> Off the top of my head:
>
> 1) Compute length of C string.
> (As usual, need to use seq on the '+1' argument.)
> 2) Scan from right to left, constructing the list.

This scan is what peekArray does now. We just need

peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
peekArray0 marker ptr = do
size <- lengthArray0 marker ptr
peekArray size ptr

and similarly for CStrings.


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise