令人困惑的SegFault涉及STL排序算法 [英] Bewildering SegFault involving STL sort algorithm

查看:60
本文介绍了令人困惑的SegFault涉及STL排序算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用STL在珍珠编程的第15列中重新创建该程序.我正在尝试使用字符串和索引向量创建后缀数组.我记录了我在名为input的字符串中读取的单词列表,该字符串用作在程序开始时从stdin读取的由''分隔的单词的列表.一切正常,直到我到达代码的排序部分为止.我想使用STL的排序算法,但对于似乎正在创建的段错误,我完全感到困惑.

I am trying to recreate the program in Column 15 of programming pearls using the STL. I am trying to create a suffix array using a string and a vector of indices. I record the list of words that I read in a string called input that acts as a list of words separated by ' ' that I read from stdin at the beginning of the program. Everything works as expected until I get to the sorting portion of the code. I'd like to use the STL's sort algorithm, but I am completely perplexed at a seg fault that I seem to be creating.

我有:

vector<unsigned int> words;

和全局变量

string input;

我定义了我的自定义比较功能:

I define my custom compare function:

bool wordncompare(unsigned int f, unsigned int s) {
  int n = 2;

  while (((f < input.size()) && (s < input.size()))
         && (input[f] == input[s])) {
    if ((input[f] == ' ') && (--n == 0)) {
      return false;
    }

    f++;
    s++;
  }

  return true;
}

当我运行代码时:

sort(words.begin(), words.end());

程序顺利退出.

但是,当我运行代码时:

However, when I run the code:

sort(words.begin(), words.end(), wordncompare);

我在STL的深处生成了一个SegFault.

I generate a SegFault deep within the STL.

GDB回溯代码如下:

The GDB back-trace code looks like this:

#0  0x00007ffff7b79893 in std::string::size() const () from /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libstdc++.so.6
#1  0x0000000000400f3f in wordncompare (f=90, s=0) at text_gen2.cpp:40
#2  0x000000000040188d in     std::__unguarded_linear_insert<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, unsigned int, bool (*)(unsigned int, unsigned int)> (__last=..., __val=90, __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1735
#3  0x00000000004018df in std::__unguarded_insertion_sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1812  
#4  0x0000000000402562 in std::__final_insertion_sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1845
#5  0x0000000000402c20 in std::sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:4822
#6  0x00000000004012d2 in main (argc=1, args=0x7fffffffe0b8) at text_gen2.cpp:70

我在另一个程序中有类似的代码,但是在该程序中,我使用的是向量而不是向量.对于我的一生,我无法弄清楚自己在做错什么.谢谢!

I have similar code in another program, but in that program I am using a vector instead of vector. For the life of me I can't figure out what I'm doing wrong. Thanks!

推荐答案

您的比较器很可能不满足严格的弱排序;例如,它违反了传递性,因为存在一些值环,使得A<B,B <C,且C <答:这是要求.我看不到它在我的头顶上方,但我将继续凝视它几分钟.

Most likely your comparator doesn't satisfy strict weak ordering; e.g., it violates transitivity because some ring of values exists such that A < B, B < C, and C < A. Here are the requirements. I don't see it off the top of my head, but I am going to keep staring at it for a few minutes.

这篇关于令人困惑的SegFault涉及STL排序算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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