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);

                    }

                }
            }
3
  • 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. Commented Apr 16, 2016 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. Commented Apr 16, 2016 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 you know. You can rewrite your code to an iteration instead of a recursive function. Commented Apr 16, 2016 at 15:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I removed the While(true). Still it hits the stackoverflow Exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.