Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bewildering SegFault involving STL sort algorithm

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.

I have:

vector<unsigned int> words;

and global variable

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;
}

When I run the code:

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

The program exits smoothly.

However, when I run the code:

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

I generate a SegFault deep within the STL.

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!

like image 533
just_wes Avatar asked Mar 14 '10 03:03

just_wes


People also ask

What causes segfault in C?

In practice, segfaults are almost always due to trying to read or write a non-existent array element, not properly defining a pointer before using it, or (in C programs) accidentally using a variable's value as an address (see the scanf example below).

What is segmentation fault in os?

A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).


1 Answers

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.

like image 62
Todd Gardner Avatar answered Sep 28 '22 01:09

Todd Gardner