Easily reproducible segfault
Status: Beta
Brought to you by:
gallesio
Hello,
just played around with stklos and found the following bug:
acer joe [~]: stklos
STklos version 0.57 [Linux-2.6.6-i686]
Copyright Š 1999-2004 Erick Gallesio - I3S-CNRS/ESSI
<eg@essi.fr>
stklos> (define (f x) (if (zero? x) 0 (+ x (f (- x 1)))))
;; f
stklos> (f 20000)
Segmentation fault
acer joe [~]:
Same thing also happens at a friend's PC using also 0.57.
Greetings,
Joe
Logged In: YES
user_id=50465
Correct me if I'm wrong, but I believe your definition of f
is not tail-recursive, and hence (f x) will build x frames
on the stack before collapsing the whole thing. If x is
large, things can blow up.
One might then fruitfully argue whether a segfault is the
appropriate response, or if it should check for this
condition. There might be efficiency considerations.
Here's a rough draft of a tail-recursive version of f, which
requires a helper f1. This one (if I did it right) will get
the same answer but the stack space required does not depend
on x.
(define (f1 x y) (if (zero? x) y (f1 (- x 1) (+ x y))))
(define (f x) (f1 x 0))