代码之家  ›  专栏  ›  技术社区  ›  mikestram

堆栈溢出错误-C#

  •  -4
  • mikestram  · 技术社区  · 11 年前

    我一直收到堆栈溢出错误,但我无法找出原因或如何修复它。以下是我的代码:

     private void ShowDiff(int leftIndex, int rightIndex)
            {
                if (leftIndex > 0 && rightIndex > 0 &&
                    _compareFunc(_left[_preSkip + leftIndex - 1], _right[_preSkip + rightIndex - 1]))
                {
                    ShowDiff(leftIndex - 1, rightIndex - 1);
                    FireLineUpdate(DiffType.None, _preSkip + leftIndex - 1, -1);
                }
                else
                {
                    if (rightIndex > 0 &&
                        (leftIndex == 0 ||
                         _matrix[leftIndex, rightIndex - 1] >= _matrix[leftIndex - 1, rightIndex]))
                    {
                        ShowDiff(leftIndex, rightIndex - 1);
                        FireLineUpdate(DiffType.Inserted, -1, _preSkip + rightIndex - 1);
                    }
                    else if (leftIndex > 0 &&
                             (rightIndex == 0 ||
                              _matrix[leftIndex, rightIndex - 1] < _matrix[leftIndex - 1, rightIndex]))
                    {
                        ShowDiff(leftIndex - 1, rightIndex);
                        FireLineUpdate(DiffType.Deleted, _preSkip + leftIndex - 1, -1);
                    }
                }
    
            }
    

    这是我一直遇到的错误:

    Comparer.exe中发生“System.StackOverflowException”类型的未处理异常

    感谢任何帮助。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Jodrell    11 年前

    编辑: 添加了中断,以防没有条件为真。


    相反,使用循环重写函数。此外,您可以稍微简化逻辑。

    注:假设两者 leftIndex rightIndex 大于 0 持续存在。

     private void ShowDiff(int leftIndex, int rightIndex)
     {
         while(leftIndex > 0 && rightIndex > 0)
         {
             if (leftIndex > 0 && rightIndex > 0 &&
                    _compareFunc(
                        _left[_preSkip + leftIndex - 1],
                        _right[_preSkip + rightIndex - 1]))
             {
                 leftIndex--;
                 rightIndex--;
                 FireLineUpdate(
                     DiffType.None,
                     _preSkip + leftIndex - 1,
                     -1);
             }
             else
             {
                 if (rightIndex > 0 &&
                        (_matrix[leftIndex, rightIndex - 1] >=
                            _matrix[leftIndex - 1, rightIndex]))
                 {
                     rightIndex--;
                     FireLineUpdate(
                         DiffType.Inserted,
                         -1,
                         _preSkip + rightIndex - 1);
                 }
                 else if (_matrix[leftIndex, rightIndex - 1] <
                             _matrix[leftIndex - 1, rightIndex]))
                 {
                     leftIndex--;
                     FireLineUpdate(
                         DiffType.Deleted,
                         _preSkip + leftIndex - 1,
                         -1);
                 }
                 else
                 {
                     break;
                 }
             }
         }
     }