[Haskell-cafe] Strange results when trying to create large Bool arrays.

Richard Kelsall r.kelsall at millstream.com
Wed Jul 11 08:57:57 EDT 2007

My first ever Haskell program just creates an array in memory.
I wanted to try creating really big arrays.

import Data.Array.IO
import System
import Text.Printf

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. I guess there must be a switch to make it produce a nice
error message rather than overflowing without warning.

If I replace all the 'Int's in the program with 'Integer's it
works better, but sometimes it freezes when I give it a silly large
number rather than returning an 'unable to allocate that much memory'

And what really puzzles me is this behaviour in the 'Integer' version.

1000 created array
1000000 created array
1000000000 created array
10000000000 created array  (approximately my bits of real memory)
100000000000 created array
1000000000000 freezes
10000000000000 created array
100000000000000 created array
1000000000000000 freezes
10000000000000000 created array

It seems to randomly claim to have successfully created huge sizes
of array. So now I am not sure which of these arrays it is really
creating. I would expect it to consistently freeze above my real
memory size. Or preferably return a 'not enough memory' error

I have 1Gb of real memory in my machine. GHC 6.6.1 for Windows,
running on W2K.

Assuming these errors are in Haskell rather than my code (beginner's
fallacy?) could I vote for having the memory, bounds, overflow etc
checking switched on by default. I like my errors to be made as
visible as possible as soon as possible.

Or did I do something wrong?


More information about the Haskell-Cafe mailing list