错误“内存限制”(无限循环,C#)

时间:2019-02-17 16:13:18

标签: c#

我的代码有问题。编译器显示错误“ MemoryLimit”。我想,这个错误是由于无限循环造成的。

我认为,“ DoSmth”中的运算符“ if else”会导致此错误,但是我不知道该如何纠正。我尝试了这种方法的几种方法,但没有任何反应。

您能帮我修改我的代码吗?谢谢。

using System.Collections.Generic;
using System.Linq;

namespace TableParser
{
    public class FieldsParserTask
    {
        public static readonly char[] FieldSeparators =
        {
            '\"', '\'', ' '
        };

        public static void DoSmth(List<string> fields, string line, int count, bool Method)
        {
            if (Method)
            {
                fields.Add(FindField(line, count).Value);
                count = FindField(line, count).GetIndexNextToToken();
            }
            else
            {
                fields.Add(ReadField(line, count).Value);
                count = ReadField(line, count).GetIndexNextToToken();
            }
        }

        public static List<string> ParseLine(string line)
        {
            var fields = new List<string>();
            var count = 0;
            while (count < line.Length)
            {
                if (line[count] == ' ')
                    count++;
                else if (!FieldSeparators.Contains(line[count]))
                {
                    DoSmth(fields,line,count,true);
                }
                else
                {
                    DoSmth(fields,line,count,false);
                }
            }
            return fields;
        }

        public static Token FindField(string line, int count)
        {
            int length = count;
            while (length < line.Length && !FieldSeparators.Contains(line[length]))
                length++;
            string value = line.Substring(count, length - count);
            return new Token(value, count, length - count);
        }

        public static Token ReadField(string line, int count)
        {
            var value = "";
            char symbol = line[count];
            var countNow = count + 1;
            while (countNow < line.Length && line[countNow] != symbol)
            {
                if (line[countNow] == '\\')
                    countNow++;
                value += line[countNow];
                countNow++;
            }
            return new Token(value, count, countNow - count + 1);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您的ParseLine线方法中有无限循环。仅当字符为空格时,才增加count的值。您应该意识到int是一个值类型,因此当您将count传递给其他方法之一时,实际上是在传递其值的副本,因此任何更改都不会影响原始变量的值

这里是Microsoft有关值类型的文档的链接。 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types

您可以通过替换

来更新您的count变量的值
var count = 0;
while (count < line.Length)
{
    if (line[count] == ' ')
        count++;
    else if (!FieldSeparators.Contains(line[count]))
    {
         DoSmth(fields,line,count,true);
    }
    else
    {
         DoSmth(fields,line,count,false);
    }
}

var count = 0;
while (count < line.Length)
{
    if (line[count] == ' ')
        count++;
    else if (!FieldSeparators.Contains(line[count]))
    {
         count = DoSmth(fields,line,count,true).Count;
    }
    else
    {
         count = DoSmth(fields,line,count,false).Count;
    }
}