解决 C++ 中的 StackOverFlowException
我正在尝试编写一个程序,用于使用递归对 C++ (VS2010) 中的数组元素进行选择排序。对于较小的数组,该程序可以正常工作,其中数组的大小小于 10,当我将数组的大小增加到 10000 时,我面临 stackoverflow 异常。我该如何解决这个问题?
感谢您到目前为止的回答。我的想法不是对数组进行排序,而是使用递归对大数组进行排序并命中 stackoverflow 异常。我这个练习背后的主要想法是学习解决 stackoverflow 异常的方法,而不是对数组进行排序。
selectionSortRecursive(int a[], int startindex, int endindex)
{
if (startindex<endindex)
{
int s = startindex;
for (int index=startindex+1;index<endindex;index++)
{
if (a[s]>a[index])
s=index;
}
if (s>startindex)
{
a[s]=a[startindex]+a[s];
a[startindex]=a[s]-a[startindex];
a[s]=a[s]-a[startindex];
}
selectionSortRecursive(a, startindex+1, endindex);
}
}
I am trying to write a program for selection sort of the elements an array in C++ (VS2010) using recursion. The program works without any issues for for smaller arrays where the size of the array is less than 10 when I increase the size of the array to 10000 I am facing stackoverflow exception. How do I go about resolving this?
Thank you for the answers so far. My idea is not to sort the array but to sort a large array using recursion and the hit a stackoverflow exception. My main idea behind this exercise is to learn ways to resolve the stackoverflow exception rather than sorting the array.
selectionSortRecursive(int a[], int startindex, int endindex)
{
if (startindex<endindex)
{
int s = startindex;
for (int index=startindex+1;index<endindex;index++)
{
if (a[s]>a[index])
s=index;
}
if (s>startindex)
{
a[s]=a[startindex]+a[s];
a[startindex]=a[s]-a[startindex];
a[s]=a[s]-a[startindex];
}
selectionSortRecursive(a, startindex+1, endindex);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要么增加堆栈的大小(可以使用
STACK
链接器选项来完成),要么——这是一个更好的选择——改进或替换你的算法。听起来递归算法可能不适合您需要处理的数据类型,但也许您可以通过在每个方法调用中使用更少的局部变量和/或参数来改进事情,以使堆栈帧更小。Either increase the size of the stack (which can be done with the
STACK
linker option) or -- and this is a much better option -- improve or replace your algorithm. It sounds like a recursive algorithm may not be suitable for the kind of data you need to process, but maybe you can improve things by using fewer local variables and/or parameters in each method invocation, to make the stack frames smaller.您要么使用大量本地存储,要么您的递归算法会通过后续调用增加内存使用量。无论哪种情况,迭代解决方案都可能解决您的问题。
You are either using a great deal of local storage or your recursive algorithms explodes memory usage by subsequent calls. In either case probably an iterative solution would solve your problems.
您可能知道,局部变量保存在堆栈中。
当函数递归时,每次调用的局部变量在各自的函数执行期间都会保留在堆栈中。如果递归太多,累积量就会太大,最终可能会出现堆栈溢出异常(就像您所遇到的那样)或分段错误(堆栈溢出到受保护的内存中)。
这是根本性的,除了增加可用内存或将函数重写为迭代之外,没有其他方法可以解决这个问题。
Local variables, as you may know, are kept on the stack.
When a function recurses, local variables from each call remain on the stack for as long as their respective functions execute. If you recurse too much, the build-up will be too great and you will likely end up with a stack overflow exception, like you're getting, or a segmentation fault, where the stack overflows into protected memory.
This is fundamental, and there is no way around it other than to increase your available memory or to rewrite your function to be iterative.
递归方法会占用大量堆栈空间;我们可以使用
for
循环来代替它:此代码可能适合您。
The recursive approach will take lot of stack space; instead of it, we can use a
for
loop to implement it:This code might work for you.