We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › stack overflow error
Page Index Toggle Pages: 1
stack overflow error (Read 1313 times)
stack overflow error
May 22nd, 2010, 5:14pm
 
is there a way to ignore the stack overflow error(with recursion) because i have a program using recursion but it is not never ending it just takes a while.
here is a snippet i am trying to create a paint bucket.


void bucket(int _x, int _y, int _c){
 int x =_x;
 int y = _y;
 int argb = _c;//selected color
 pixels[x+y*width]=moose;//mouse color
 if(pixels[x-1+y*width]==argb){
   bucket(x-1,y,argb);
 }
   if(pixels[x+1+y*width]==argb){
   bucket(x+1,y,argb);
 }
     if(pixels[x+((y-1)*width)]==argb){
   bucket(x,y-1,argb);
 }
       if(pixels[x+((y+1)*width)]==argb){
   bucket(x,y+1,argb);
 }

}

i only call it once


Re: stack overflow error
Reply #1 - May 22nd, 2010, 10:36pm
 
It may be that the garbage collection is taking a while also.

Edit:
search wikipedia for garbage collection. I cannot link because I'm a newbie again...

According to the wiki, the stack data is generally released after the last call has returned.
Perhaps add a depth limit and call bucket() more than once, remembering where each left off?
Re: stack overflow error
Reply #2 - May 23rd, 2010, 1:05am
 
Perhaps you should try and generate less data to push in the stack:
Code:
void bucket(int x, int y, int argb){
pixels[x+y*width]=moose;//mouse color
if(pixels[x-1+y*width]==argb){
bucket(x-1,y,argb);
}
if(pixels[x+1+y*width]==argb){
bucket(x+1,y,argb);
}
if(pixels[x+((y-1)*width)]==argb){
bucket(x,y-1,argb);
}
if(pixels[x+((y+1)*width)]==argb){
bucket(x,y+1,argb);
}

}

But it is likely that such deeply recursive function will always trip Java.
Somebody had a similar issue a while ago: floodfill help (stackerror)
The solution given by toxi (and slightly improved by myself) is... not to use recursion but to manage the stack manually...
Page Index Toggle Pages: 1