
Hi! Can someone tell me, how i can check, if a Ptr is a nullPtr. Now I am using this function, but I think its not a very nice one: import Ptr isNullPtr :: (Ptr a) -> Bool isNullPtr ptr = (minusPtr ptr nullPtr) == 0 Maybe someone has got a better idea... christian

Why not: isNullPtr = (==nullPtr) ? -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume On Thu, 15 May 2003, Christian Buschmann wrote:
Hi! Can someone tell me, how i can check, if a Ptr is a nullPtr. Now I am using this function, but I think its not a very nice one:
import Ptr
isNullPtr :: (Ptr a) -> Bool isNullPtr ptr = (minusPtr ptr nullPtr) == 0
Maybe someone has got a better idea...
christian
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hal Daume III wrote:
Why not:
isNullPtr = (==nullPtr)
? Oh sure....sorry.... i tried this before, but i had a small confusion with my type-signatures in my program and it didn't work. Now i fixed my signatures and its working.... But there is one more (small) question: If i am using the "peekCString" function with a null-pointer my program crashes, because the peekCString function is trying to read a String from an illegal address. But is this normal behaviour of this function? Shouldn't it check for a null-pointer and then return an empty String, raise an exception or do something else than crashing my program?
Oh, and my compiler is ghc-5.04.2 on linux-2.4.20 thx christian

On Thu, 15 May 2003 22:07:56 +0200
Christian Buschmann
Why not:
isNullPtr = (==nullPtr)
? Oh sure....sorry.... i tried this before, but i had a small confusion with my type-signatures in my program and it didn't work. Now i fixed my signatures and its working.... But there is one more (small) question: If i am using the "peekCString" function with a null-pointer my
Hal Daume III wrote: program crashes, because the peekCString function is trying to read a String from an illegal address. But is this normal behaviour of this function? Shouldn't it check for a null-pointer and then return an empty String, raise an exception or do something else than crashing my program?
Oh, and my compiler is ghc-5.04.2 on linux-2.4.20
thx christian
It DEFINITELY should NOT return an empty string. Further, I imagine it would be pretty painful to check for a nullPointer every time you could use it (i.e. in this case and other cases, it would be arbitrary to just usi it in this case.) I'd rather it be a fast and small as possible. If I really want the checking it is trivial to write it myself. checkedPeekCString p | isNullPtr p = ioError uhoh | otherwise = peekCString p

More obvious would be: import Foreign(Ptr, nullPtr) isNullPtr :: Ptr a -> Bool isNullPtr p = p == nullPtr -- Alastair
participants (4)
-
Alastair Reid
-
Christian Buschmann
-
Derek Elkins
-
Hal Daume III