错误线程访问/ seg故障QuickSort [英] Bad thread access/seg fault QuickSort

查看:151
本文介绍了错误线程访问/ seg故障QuickSort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个排序项目,我的快速排序工作正好与300kb的数据,但是当我尝试排序1mb的数据,程序给我坏的线程访问Xcode和seg fault:11在终端。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆