Help! Stack Overflow in Recursive 
Author Message
 Help! Stack Overflow in Recursive

Hi,

I got a stack overflow error in my program.(VC++ 6.0 complier).
In this program, i try to flood an 255x255 image.
Rightnow the image is all 0. I want to set it to 1 by recursive flood.
If I change to 254x254 image, it won't crash.
Please help me!! Thanks a lot.

#include <stdiio.h>
#define s 255
char l[s][s];
int v =0;

// recursive
void r(int x, int y) {
 l[x][y] = 1;
 v ++;
 if (x > 0)
  if (l[x-1][y] == 0)
   r(x-1,y);

 if (y > 0)
  if (l[x][y-1] == 0)
   r(x,y-1);

 if (x < s - 1)
  if (l[x+1][y] == 0)
   r(x+1,y);

 if (y < s- 1)
  if (l[x][y+1] == 0)
   r(x,y+1);

Quote:
}

int main(int argc, char* argv[])
{
 int x,y;
// clear the image
 for (x=0;x<s;x++) for (y=0;y<s;y++)
  l[x][y] = 0;

 // recursive
 r(0,0);

 printf("number = %d!\n",v);
 return 0;

Quote:
}

Justin


Wed, 30 Jul 2003 01:22:46 GMT  
 Help! Stack Overflow in Recursive

Quote:

> Hi,

> I got a stack overflow error in my program.(VC++ 6.0 complier).
> In this program, i try to flood an 255x255 image.
> Rightnow the image is all 0. I want to set it to 1 by recursive flood.
> If I change to 254x254 image, it won't crash.
> Please help me!! Thanks a lot.

Believe it or not, this is an implementation-specific question, not a C
question.  Your implementation has insufficient stack space for the
depth of recursion your program requires.  

A better solution is to make the routine iterative.  If you really think
that recursion is appropriate, how to increase the stack size -- if
possible -- is something your documentation should tell you.  If that
does not suffice, post your question to one of the many Microsoft- and
VC-specific newsgroups.  Note that the C programming language does not
even specify a stack be used, much less how to change its size.



Wed, 30 Jul 2003 01:45:57 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help! Stack Overflow in Recursive

2. How to Prevent Stack Overflows with Recursive Routines?

3. prevent stack overflow in recursive function

4. Stack Overflow, with recursive function

5. Stack Structure and Stack Overflow for CE

6. stack overflow help?

7. stack overflow help

8. Stack overflow help sought

9. C++ Builder Stack Overflow Error - Please help - Need to know how to overcome this Debugger Exception

10. HTML help gives a stack overflow?

11. need help with stack overflow

12. Stack Overflow ( HELP PLEASE )

 

 
Powered by phpBB® Forum Software