基数排序分段故障

时间:2014-08-02 03:16:12

标签: c++ linux radix-sort

所以我这里有一个程序用于检查基数排序的运行时间。在我的Windows机器上进行测试时,一切都运行良好,但是当我将它带到学校的Linux机器(需要运行)时,我会遇到一个神秘的分段错误。基数排序算法适用于处理来自此处的矢量:http://en.wikipedia.org/wiki/Radix_sort#Example_in_C.2B.2B

我假设我的问题来自于我处理向量的方式,因为我还是他们的新手。我尝试通过GDB运行它,我不确定我在这里看到了什么。

Program received signal SIGSEGV, Segmentation fault.
0x0804983e in std::vector<int, std::allocator<int> >::operator[] (
    this=0xfffeebd0, __n=18) at /usr/include/c++/4.6/bits/stl_vector.h:696
696           { return *(this->_M_impl._M_start + __n); }
(gdb) bt
#0  0x0804983e in std::vector<int, std::allocator<int> >::operator[] (
    this=0xfffeebd0, __n=18) at /usr/include/c++/4.6/bits/stl_vector.h:696
#1  0x08049202 in radix (nums=..., length=135041, largest=0)
    at RadixSort2.cpp:98
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

我在radix函数中包含了几个测试打印语句。每次打印“TEST ME 1”行,但“test me 2”行不太一致。有时它根本不会打印,有时会打印几次。我认为我见过的最多是8次打印。这让我更加困惑,因为在第一个测试语句和第二个测试语句之间发生的唯一事情是for循环的开始,但是还没有什么应该执行,所以为什么有些情况下它根本不运行?基数功能将在下方发布,完整代码可在此处找到:https://gist.github.com/Shoggoth269/7f6645d01eaf5ba91617

void radix(vector<int> & nums, int length, int largest)
{
    list<int> bucket[10];
    int i;

    for(int n = 1; largest >= n; n *= 10)
    {
        cout << endl << "TEST ME 1" << endl;
        for(i = 0; i < length; i++)
        {
            cout << endl << "test me 2";
            bucket[(nums[i]/n)%10].push_back(nums[i]);
        }

    for (int k = i = 0; i < 10; bucket[i++].clear())
        for(list<int>::iterator j = bucket[i].begin(); j!=bucket[i].end(); nums[k++] = *    (j++));
    }
}

0 个答案:

没有答案
相关问题