1

I am doing a recursive program and during the execution of a recursive function it shows stack overflow error. I cannot proceed without completing this recursive function. Please some one help me... This is the code that i have done:

   public void blob(int k, int l, int[,] MV1)
    {
        while (true)
        {
            if ((MV1[k, l] == 1) && (status[k, l] != 1))
            {
                count = count + 1;
                if (count < 6000)
                {
                    if (k < xmin)
                    {
                        X[Xr, 0] = k;
                        xmin = k;
                    }
                    if (l < ymin)
                    {
                        Y[Yr, 0] = l;
                        ymin = l;
                    }
                    if (k > xmax)
                    {
                        X[Xr, 1] = k;
                        xmax = k;
                    }
                    if (l > ymax)
                    {
                        Y[Yr, 1] = l;
                        ymax = l;
                    }
                    status[k, l] = 1;


                    if (l != (MV1.Length / MV1.GetLength(0)) - 1)
                    {
                        blob(k, l + 1, MV1);
                    }
                    if ((l != 0))
                    {

                        blob(k, l - 1, MV1);

                    }
                    if (k != MV1.Length - 1)
                    {
                        blob(k + 1, l, MV1);
                    }
                    if ((k != 0))
                    {

                        blob(k - 1, l, MV1);

                    }

                }
            }
rory.ap
  • 34,009
  • 10
  • 83
  • 174
Sreeraj
  • 21
  • 4
  • I've rolled back your edit. You can't modify your question to change it substantively once an answer has been made. The answer would no longer apply after your edit. If you have a *new* question, then ask a different question, but this question must now remain as is. If you have clarifications to your question which don't substantively change it, then feel free to modify it. – rory.ap Apr 16 '16 at 12:57
  • Actually I wrote the while to check the stack overflow error. But before asking you the question i forgot to remove while(true). That is what actually happened. – Sreeraj Apr 16 '16 at 13:22
  • The stack probably gets overflown because you are putting the array MV1 (at most) 6000 times on the stack. The [stack is limited](http://stackoverflow.com/questions/28656872/why-is-stack-size-in-c-sharp-exactly-1-mb) you know. You can rewrite your code [to an iteration instead of a recursive function](http://stackoverflow.com/questions/159590/way-to-go-from-recursion-to-iteration). – venerik Apr 16 '16 at 15:02

1 Answers1

1

The problem is your method never exits the while loop because you have while (true). Therefore, your recursive algorithm keeps calling itself deeper and deeper until it runs out of space on the stack.

You need to make it so your loop exits at some point, either from within using return or preferably with a better condition in your while statement.

Note, it's generally considered bad practice to use while (true). You want to avoid doing so unless absolutely necesssary.

rory.ap
  • 34,009
  • 10
  • 83
  • 174