如何在递归选择排序算法中调用类析构函数?
RecursiveSort::RecursiveSort(int myArray[], int first, int arraySize)
{
int smallest = first, j;
if (smallest < arraySize)
{
smallest = first;
for (j=first+1; j<arraySize; j++)
{
if (myArray[j] < myArray[smallest])
{
smallest = j;
}
}
swap(myArray[first], myArray[smallest]);
first++;
RecursiveSort::RecursiveSort(myArray, first, arraySize);
}
};
在我的 main() 中;我将调用 RecursiveSort sort(myArray, 0, arraySize);
当 arraySize > 时,出现堆栈溢出4000,程序崩溃了。是否可以在某处调用类析构函数以防止堆栈溢出?我尝试使用“发布”而不是“调试”(项目属性>配置管理器>配置下拉菜单)。然而,当我尝试集成用于测量排序所需时间的“TimeStamp_Lib.lib”库时,这会导致其他问题。
任何意见/建议将不胜感激,谢谢!
RecursiveSort::RecursiveSort(int myArray[], int first, int arraySize)
{
int smallest = first, j;
if (smallest < arraySize)
{
smallest = first;
for (j=first+1; j<arraySize; j++)
{
if (myArray[j] < myArray[smallest])
{
smallest = j;
}
}
swap(myArray[first], myArray[smallest]);
first++;
RecursiveSort::RecursiveSort(myArray, first, arraySize);
}
};
and in my main(); i will call RecursiveSort sort(myArray, 0, arraySize);
There is a stack overflow when arraySize > 4000, and the program crashes. Is it possible to call class destructors somewhere to prevent stack overflow? I have tried using "release" instead of "debug" (project properties > configuration manager > configuration pull-down menu). However this causes other issues when I try to integrate a "TimeStamp_Lib.lib" library which is used to measure how long the sort takes.
Any advice/suggestions would be greatly appreciated, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将其设为自由函数,这样就不会创建任何对象。如果由于某种原因算法必须在类的构造函数中运行,您可以从构造函数中调用它。
也就是说,除非编译器将递归优化为迭代本身,否则递归将不适合需要那么深入的算法。要对 4000 个整数进行排序,您的函数会深入 4000 层,这会占用大量堆栈空间。相比之下,快速排序的深度约为 log2(4000) = 12 层。
You should make this a free function, so no objects would be created. If, for some reason the algorithm has to run in a constructor of a class, you can call it from the constructor.
That said, unless the compiler optimizes the recursion into iteration itself, recursion won't be suitable for algorithms that require going that deep. To sort 4000 integers, your function goes 4000 levels deep which eats a lot of stack space. By comparison, a quick sort goes some log2(4000) = 12 levels deep.
该函数中没有分配 - 你的问题不是你分配了太多内存。
我想你递归了太多次。这将需要某种形式的算法更改,或者只是限制使用此方法可以排序的项目数量。如果您可以以编译器可以优化递归的方式更改此设置,那么这是可行的(尽管它可能是特定于实现的)。更好的是,您可以自己删除递归并将其更改为基于堆栈的循环。 这里有关此的更多信息
There are no allocations in that function - your issue is not that you have allocated too much memory.
I'd imagine you are recursing too many times. That will require some form of algorithm change, or just a limit on how many items you can sort with this method. If you can change this in such a way that the compiler can optimise away the recursion, that would work (though it could be implementation specific). Even better, remove the recursion yourself and change it to a stack-based loop. Here's some more information about that
是的,可以手动调用析构函数。然而,它们销毁一个对象,但不会释放其内存。递归很容易思考,但在现实生活中却不太有用。尝试将所有对象移至堆栈外部的池中,或者更好地以迭代方式工作。
Yes, destructors can be manually invoked. However, they destroy an object, they don't free its memory. Recursion is easy to think in terms of, but its not so useful in real life. Try moving all your objects to a pool outside the stack, or better yet work in an iterative way.