错误线程访问/ seg故障QuickSort [英] Bad thread access/seg fault QuickSort
问题描述
void SortingCompetition :: quicksort(int low,int high)
{
if(high!= low&& high> low)
{
long one = hash [low]
long two = hash [high];
long three = hash [high / 2];
if((< = two& one> = three)||(one< = three& one> = two))
{
swap(hash [low] ,hash [high]);
swap(copyOfWords [low],copyOfWords [high]);
}
else if((three <= a& three> = two)||(three< = two& three> = 1))
{
swap(hash [high / 2],hash [high]);
swap(copyOfWords [high / 2],copyOfWords [high]);
}
else
{
}
int i = low;
int j = high-1;
while(i!= j&& i< j)
{
while(hash [i] <= hash [high]&& i {
i ++;
}
while(hash [j]> = hash [high]&& i< j)
{
j--
}
if(i == j || i> j)
{
}
else
{
swap ],hash [j]);
swap(copyOfWords [i],copyOfWords [j]);
}
}
swap(hash [i],hash [high]);
swap(copyOfWords [i],copyOfWords [high]);
quicksort(low,j-1);
quicksort(j + 1,high);
}
哈希和副本都动态分配到相同的大小。我不知道如何解决这个问题。提前感谢
它是一种递归方法。所以你是通过调用溢出堆栈。
每当你调用一个函数,包括递归,返回地址和通常的参数被推送到调用堆栈。堆栈是有限的,所以如果递归太深,你最终会用完堆栈空间。
I'm working on a sorting project and my quick sort works just fine with 300kb of data, but when i try to sort 1mb of data, program gives me bad thread access in Xcode and seg fault:11 in terminal.
void SortingCompetition::quicksort(int low, int high)
{
if (high!=low&& high>low)
{
long one=hash[low];
long two=hash[high];
long three = hash[high/2];
if((one<=two&&one>=three)||(one<=three&&one>=two))
{
swap(hash[low], hash[high]);
swap(copyOfWords[low], copyOfWords[high]);
}
else if((three<=one&&three>=two)||(three<=two&&three>=one))
{
swap(hash[high/2], hash[high]);
swap(copyOfWords[high/2], copyOfWords[high]);
}
else
{
}
int i=low;
int j=high-1;
while(i!=j&&i<j)
{
while(hash[i]<=hash[high]&&i<j)
{
i++;
}
while(hash[j]>=hash[high]&&i<j)
{
j--;
}
if(i==j||i>j)
{
}
else
{
swap(hash[i],hash[j]);
swap(copyOfWords[i],copyOfWords[j]);
}
}
swap(hash[i],hash[high]);
swap(copyOfWords[i], copyOfWords[high]);
quicksort(low, j-1);
quicksort(j+1, high);
}
both hash and copyofwords are dynamically allocated to the same size. I don't know how to fix this problem. Thanks in advance
Its a recursive method. So you are overflowing the stack with the calls.
Whenever you call a function, including recursively, the return address and often the arguments are pushed onto the call stack. The stack is finite, so if the recursion is too deep you'll eventually run out of stack space.
这篇关于错误线程访问/ seg故障QuickSort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!