Segmentation fault

Alastair Reid reid@cs.utah.edu
22 May 2002 23:50:18 +0100


>  Hi, I am getting a segmentation fault , when I execute a code in
> hugs.  (only some times) What are the conditions that give rise to a
> segmentation fault , when executing a Haskell code in hugs?  I would
> appreciate very much if some could spare some time to help me.

It is almost always a stack overflow.  If it's not a stack overflow,
it's a bug in Hugs but these are fairly rare.

Stack overflows usually come from two sources:

1) Infinite loops, infinite recursion, etc.

2) Surprisingly deep stacks caused by the effects of lazy evaluation.
   For example, if you run Hugs with a sufficiently large heap then
   you can see this:

     Prelude> foldl (+) 0 [1..1000000]

     Segmentation fault

   This sort of error is accompanied by large heap usage so if you run
   with a smaller heap then it is possible that you'll run out of heap
   before you run out of stack.  (It uses a lot of heap because what
   is happening is that Hugs builds a thunk representing the result
   in the heap and then recursively evaluates it using the stack.)

   To fix this, use a compiler with heap profiling (like ghc or nhc)
   and insert strictness annotations.  For example, for this well known
   example, you'd use foldl' instead of foldl:

     Prelude> foldl' (+) 0 [1..1000000]
     500000500000

   (Note that foldl' is defined in the Prelude but not exported so
   you'll need to cut'n'paste it if you want to use it in your own code.)

Hope this helps,

-- 
Alastair Reid        reid@cs.utah.edu        http://www.cs.utah.edu/~reid/