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.

#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.

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

 Page 1 of 1 [ 2 post ]

Relevant Pages