how to avoid this stackoverflow?

Discussion of Common Lisp

how to avoid this stackoverflow?

Postby The_Ash » Sun May 27, 2012 11:20 am

Hi!
consider:
Code: Select all
(defstruct A
    left
    right)

(setf i (make-a)
        j (make-a :left i))

at this point
Code: Select all
>j
#S(A :LEFT #S(A :LEFT NIL :RIGHT NIL) :RIGHT NIL)


now obviously adding
Code: Select all
(setf (a-right i) j)

will result in a stack overflow,since
Code: Select all
>j
#S(A :LEFT #S(A :LEFT NIL :RIGHT #S(A :LEFT #S(A :LEFT NIL :RIGHT ..... and so on forever

now is there a way to avoid that?
if that helps:i encountered that problem as i was trying to make a BST.i wanted each node of the tree to have 3 fields,one to point to its parent,
two to point to its children...
thanks a lot for your help!
The_Ash
 
Posts: 6
Joined: Sun Mar 04, 2012 10:21 am

Re: how to avoid this stackoverflow?

Postby wvxvw » Sun May 27, 2012 1:47 pm

The stack overflow is caused by the REPL trying to print the object (but it's fine to have it like you designed it, it doesn't cause stack overflow on its own). You will need to define method print-object to prevent recursive printing of A. While defining it, you will need to take care of *print-circle* - it must be set to T as shown here: http://clhs.lisp.se/Body/v_pr_cir.htm#STprint-circleST so that objects aren't printed repeatedly.
wvxvw
 
Posts: 125
Joined: Sat Mar 26, 2011 6:23 am

Re: how to avoid this stackoverflow?

Postby The_Ash » Sun May 27, 2012 2:50 pm

Thanks a lot for your help wvxvw!
The_Ash
 
Posts: 6
Joined: Sun Mar 04, 2012 10:21 am


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron