|
Re: peekCString stack overflow: msg#00008lang.haskell.glasgow.bugs
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> |
|---|---|---|
| Previous by Date: | Re: peekCString stack overflow, Alastair Reid |
|---|---|
| Next by Date: | RE: peekCString stack overflow, Simon Marlow |
| Previous by Thread: | Re: peekCString stack overflow, Alastair Reid |
| Next by Thread: | Re: peekCString stack overflow, George Russell |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |