【问题标题】:Memory overflow [closed]内存溢出[关闭]
【发布时间】:2016-04-13 14:46:59
【问题描述】:
  List<int> pal=new List<int>();
        List<int> nums = new List<int>();
        var thdigit1 = 1;
        var thdigit2 = 999;
        while (thdigit2>=1)
        {
            var holder=thdigit1* thdigit2;
            nums.Add(holder);
            thdigit2 -= 1;
            if (thdigit2==0)
            {
                thdigit2 = 999;
                thdigit1 += 1;
            }
        }

我在列表中遇到了太多的内存过载问题。我的计算机可以处理一定的限制吗?谢谢

【问题讨论】:

  • 由于您的计算机可能不是无限机器:是的,肯定有一定的限制。但是您的代码中可能存在问题。见伊恩的回答。
  • 连同参考下面的答案请阅读stackoverflow.com/help/how-to-ask
  • 所以我相信这个问题列表应该包含 999^2 个整数,是不是太多了?
  • 请不要提供自定义错误描述 - 提供编译器/运行时报告的准确错误。

标签: c# memory


【解决方案1】:

问题是当thdigit21时,它还在继续while循环,

while (thdigit2>=1)

减少1,然后变成0

thdigit2 -= 1;

并且...重置回999

if (thdigit2==0)
{
    thdigit2 = 999;
    thdigit1 += 1;
}

因此你会出现内存溢出,因为你永远不会退出while 循环。

要修复它,您可能需要在 while 循环中添加另一个终止条件:

while(thdigit2 >= 1 && thdigit1 <= 10) //example

【讨论】:

  • 代码的目的是创建两个三位数的所有可能组合。
  • 我真的很笨,谢谢你的回答。第一次问问题,经验不足。
  • @Kieran 没问题。 :) 每个人都必须从某个地方开始..
猜你喜欢
  • 2016-05-11
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多