Quick.exe 中 0x003714e9 处的未处理异常:0xC00000FD:堆栈溢出
Unhandled exception at 0x003714e9 in Quick.exe: 0xC00000FD: Stack overflow
我从来没有遇到过这种情况,我也不知道如何解决。它只是说 "Unhandled exception at 0x003714e9 in Quick.exe: 0xC00000FD: Stack overflow." 然后中断并突出显示括号并在其旁边显示一个箭头,我认为这意味着错误位于此处。
P.S。括号是粗体
#include<iostream>
using namespace std;
int partition(int data[], int left, int right)
**{**
int pivot = data[left];
while(true)
{
while(data[left] < pivot)
{
left++;
}
while (data[right]>pivot)
{
//find smaller value than pivot from top array
right--;
}
if(left < right)
{
//change pivot place
int temp = data[right];
data[right] = data[left];
data[left] = temp;
}
else
{
return right;
}
}
}
void quickSort (int *data, int left, int right)
{
if(left<right)
{
int cut = partition(data, left, right);
if(cut>1)
{
quickSort(data, left, right);
}
if(cut+1<right)
{
quickSort(data, cut+1, right);
}
}
}
void quickSort(int *data, int length)
{
quickSort(data, length-length, length-1);
}
void print_array(int array[], int size) //this function is to print the array after we finish sorting and we can use it before the sorting begin
{
int j;
for (j=0; j<size; j++)
cout <<" "<< array[j]<<endl;
}//end of print_array
int main()
{
const int size = 5;
int arr[size]= {1, 17, 4, 6, 20};
quickSort(arr, 0, size);
print_array(arr, size);
system("pause");
return 0;
}
我觉得主要问题出在这里:
if(cut>1)
{
quickSort(data, left, right);
}
您在 left/right 之间一次又一次地调用该函数。也许你应该在这里使用 cut
而不是 right
?实际上,比较 cut
和 left
然后就像你用 right
...
修复第一个问题后的"corrupted stack"错误是因为main()
调用quickSort函数时参数错误——应该是
quickSort(arr, 0, size-1);
或
quickSort(arr, size); // the overload handling the -1
另外,重载函数中的length-length
表达式写法比较绕0
.
您还可以更改代码以实际将大小设为 right
但始终从 right-1
开始(这是采用迭代器的标准算法通常的工作方式,允许传递 end()
在最后一个数组项之后)。
我从来没有遇到过这种情况,我也不知道如何解决。它只是说 "Unhandled exception at 0x003714e9 in Quick.exe: 0xC00000FD: Stack overflow." 然后中断并突出显示括号并在其旁边显示一个箭头,我认为这意味着错误位于此处。 P.S。括号是粗体
#include<iostream>
using namespace std;
int partition(int data[], int left, int right)
**{**
int pivot = data[left];
while(true)
{
while(data[left] < pivot)
{
left++;
}
while (data[right]>pivot)
{
//find smaller value than pivot from top array
right--;
}
if(left < right)
{
//change pivot place
int temp = data[right];
data[right] = data[left];
data[left] = temp;
}
else
{
return right;
}
}
}
void quickSort (int *data, int left, int right)
{
if(left<right)
{
int cut = partition(data, left, right);
if(cut>1)
{
quickSort(data, left, right);
}
if(cut+1<right)
{
quickSort(data, cut+1, right);
}
}
}
void quickSort(int *data, int length)
{
quickSort(data, length-length, length-1);
}
void print_array(int array[], int size) //this function is to print the array after we finish sorting and we can use it before the sorting begin
{
int j;
for (j=0; j<size; j++)
cout <<" "<< array[j]<<endl;
}//end of print_array
int main()
{
const int size = 5;
int arr[size]= {1, 17, 4, 6, 20};
quickSort(arr, 0, size);
print_array(arr, size);
system("pause");
return 0;
}
我觉得主要问题出在这里:
if(cut>1)
{
quickSort(data, left, right);
}
您在 left/right 之间一次又一次地调用该函数。也许你应该在这里使用 cut
而不是 right
?实际上,比较 cut
和 left
然后就像你用 right
...
修复第一个问题后的"corrupted stack"错误是因为main()
调用quickSort函数时参数错误——应该是
quickSort(arr, 0, size-1);
或
quickSort(arr, size); // the overload handling the -1
另外,重载函数中的length-length
表达式写法比较绕0
.
您还可以更改代码以实际将大小设为 right
但始终从 right-1
开始(这是采用迭代器的标准算法通常的工作方式,允许传递 end()
在最后一个数组项之后)。