stackoverflow 异常未处理
在下面的代码中,它应该乘以 2 个数字。它适用于 3 位和少于 3 位的数字,但是当我给出 4 位或更大的数字时,它会给出运行时错误:stackoverflow 异常未处理
。我已经评论过问题出在哪里了。我认为问题是在 int
中定义变量并长时间更改它们,但问题仍然存在。错误在哪里?
编辑: 现在,你对这个问题有什么看法?它没有任何作用
public long Prod2(long u, long v)
{
var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1),(int)Math.Floor(Math.Log10(v) + 1));
int threshold = 3;
if (u == 0 || v == 0)
{
return 0;
}
else if (n <= threshold)
{
return u * v;
}
else
{
int m = (int)Math.Ceiling(n / 2.0);
int x = (int)(u / Math.Pow(10, m));
int y = (int)(u % Math.Pow(10, m));
int w = (int)(u / Math.Pow(10, m));
int z = (int)(v % Math.Pow(10, m));
long r = Prod2(x + y, w + z);
long p = Prod2(x, w);
long q = Prod2(y, z);
return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
long result = Prod2(numbers[0], numbers[1]);
textBox1.Text = result.ToString();
}
}
In the code below it should multiply 2 numbers. It works for 3 and less than 3 digits numbers but when I give numbers with 4 digits or bigger it gives runtime error: stackoverflow exception was unhandled
. I've commented where the problem is. I thought the problem is for defining variables in int
and changed them in long but the problem still exists. Where is the mistake?
edited:
now,whats do you think about the problem?it doesnt do anything
public long Prod2(long u, long v)
{
var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1),(int)Math.Floor(Math.Log10(v) + 1));
int threshold = 3;
if (u == 0 || v == 0)
{
return 0;
}
else if (n <= threshold)
{
return u * v;
}
else
{
int m = (int)Math.Ceiling(n / 2.0);
int x = (int)(u / Math.Pow(10, m));
int y = (int)(u % Math.Pow(10, m));
int w = (int)(u / Math.Pow(10, m));
int z = (int)(v % Math.Pow(10, m));
long r = Prod2(x + y, w + z);
long p = Prod2(x, w);
long q = Prod2(y, z);
return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
long result = Prod2(numbers[0], numbers[1]);
textBox1.Text = result.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此时您将进入无限递归循环,
我注意到仅当
intn > > 时才执行此行。 3
,那么也许你有一个逻辑错误?更新:读完你的评论后,我可以看到这个测试的目的是说“如果这个字符串的长度<= 3那么......”而事实上,正如所写的那样,它实际上是在说“如果转换后的字符串的 VALUE <= 3 则...”
You are getting into an infinite recursive loop at this point
I notice this line is executed only when
intn > 3
, so perhaps you have a logic bug there?Update: After reading your comments I can see that this test is intended to say "if the length of this string is <= 3 then..." when in fact as written it is actually saying "if the VALUE of this converted string is <= 3 then..."
编辑:我已经为您完全翻译了书中描述的算法:
为了获得正确的结果,您可以从一些其他方法中调用此方法,如下所示:
因此在您的事件处理程序中,例如对于
button1
,您需要执行这个 来进行调用:不要修改我的
Prod2
,只需将其粘贴到您的代码。这样,Prod2 就会进行计算,然后您的button1_Click
控制输入以及如何处理输出。EDIT: I've completely translated the algorithm described in the book for you:
To get the right result, you'd call this method from some other method like this:
So in your event handler, for example for
button1
, you'd do this to make the call:Don't modify the
Prod2
that I have, and just paste it with your code. This way, Prod2 does the calculation and then yourbutton1_Click
controls the input and what to do with the output.简而言之,您可能会遇到以下情况(根据输入而变化):
只要
textBox7
中的数字 > > 3、即未闭合的递归循环,这必然会成为stackoverflow。在有问题的行上放置一个断点,您很快就会发现问题。在不知道你的方法做什么的情况下(我不认识该算法),我无法帮助清理它,但第一步可能是有一个 get out 子句有条件地从函数返回。但是,我还看到您在使用输入参数
u
和v
之前覆盖了它们,所以也许您在算法中犯了错误?In a nutshell you have a potential (varying on input) case of:
so long as the numbers in
textBox7
are > 3, i.e. an unclosed recursion loop, which will inevitably become a stackoverflow.Put a breakpoint on the line in question and you'll quickly see the problem. Without knowing what your method does (I don't recognise the algorithm) I can't help much about cleaning it up, but the first step might be to have a get out clause to return from the function conditionally. However, I also see you overwriting the input arguments
u
andv
before they get used so perhaps you've made a mistake in the algo?Pow 方法返回 double,所以我认为你的 x、y、z、w 和 z 也应该声明为 double。
Pow method returns double, so I think your x, y, z, w, and z should be declared as double as well.
您将获得递归调用的“StackoverFlow”。最好对代码中发现的漏洞进行模制。我建议你改变一下逻辑。
静态 int 调用栈 = 0;
you are getting "StackoverFlow" for the Recursive call. its better to mold the holes found in the code. I suggest you to change the logic.
static int callStack = 0;