Richard Kelsall wrote:

main = do
    n <- getArgs >>= readIO . head :: IO Int
    a <- newArray (1,n) True :: IO (IOUArray Int Bool)
    printf "Created array 1 .. %8d \n" (n::Int) :: IO ()

It appears to work up to quite large numbers, but then gets strange.
When I give it an array size of 1,000,000,000,000 it returns this

Created array 1 .. -727379968

Presumably the Int has overflowed without warning when read from the
argument.

Yes, that's right.

I guess there must be a switch to make it produce a nice
error message rather than overflowing without warning.

Actually, there isn't. Int is a bit of an odd fish that way; it's a window onto the underlying machine's behaviour, not a tidy, well-behaved mathematical ring. In a similar vein, I was initially perplexed when I found that an expression like this produces garbage instead of an error:

  read "11111111111111111111111111111111111" :: Int

I have not seen a lot of interest expressed in fixing this sort of misbehaviour, which jars a little with the usual emphasis on stringency and testing.

It seems to randomly claim to have successfully created huge sizes
of array.

This may be outside of Haskell's control, as you're not actually touching the memory you allocate. I wouldn't be surprised if the underlying page allocation is succeeding by virtue of the OS being willing to overcommit resources that may not actually be used. (This would be normal behaviour on Linux, for example.) In such a case, the Haskell runtime may not receive an error until you try to actually touch the data.

You can get GHC to fix an upper limit on the heap size it will try to use, by passing "+RTS -M768m -RTS" to your compiled program on the command line. That should cause your program to crash more reliably.

        <b
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to