12

Std :: sort throws Segmentation Fault C ++

 3 years ago
source link: https://www.codesd.com/item/std-sort-throws-segmentation-fault-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Std :: sort throws Segmentation Fault C ++

advertisements

Here's my code

bool cmp (const char &a, const char &b)
{
    if ((int) a == (int) b)
    {
        return false;
    }

    if ((int) a > (int) b)
    {
        return false;
    }
    return true;
}
std::sort(
        dfaVector.at(0).getSigma().begin(),
        dfaVector.at(0).getSigma().end(),
        cmp);

getSigma() returns std::vector<char> , and they aren't empty - I checked it. I can post stack trace from gdb if you want. I'm using g++ 4.8, OS Mint 14

ANSWER

As suggested by @livingissuicide the problem was that getSigma() needs to return a reference(i.e. sth constant, @PhoenixX_2). The explanation why it needs to return a reference (and why just a simple copy doesn't suffice) is because

The problem is that there are two calls to getSigma, producing two different vectors. A pair of iterators passed to sort is not a valid range - the two iterators point into different containers.

Explanation courtesy of @IgorTandetnik .


getSigma() returns std::vector< char >

Make sure your getSigma() function returns a reference (std::vector< char >&) rather than a copy!

Related Articles

Segmentation Fault When Checking Pointer

I am writing a program that reads a dictionary file (text file, one word per line), and inserts it into a structure consisting of a "linked list" of arrays, where the word is recursively forwarded to the [first letter - 'a'] entry of an array (w

Palindrome of string gets a segmentation fault

Just for fun I have written a function to check if string given is palindrome. When I run the prog it throws segmentation fault. Could anyone please throw light on it. int palindrome( const char *input ) { char * reverse; int len = 0 ; int i = 0; boo

Vector :: size and Segmentation fault

Why could this code throw segmentation fault?:/ listeners = new vector<Listener*> (); ... /* other code */ if (listeners != NULL) { int i = listeners->size(); } Just because the pointer isn't NULL doesn't mean it points to a valid vector<Liste

Segmentation Fault Error During Matrix Basic Operation

The following code is throwing Segmentation fault (core dumped) error when I run it. The code is compiled with g++ struct SomeClass { int *available; int **need; int **allocation; } SomeClass::SomeClass(int nR, int nT) { available = new int[nR]; for

Segmentation Fault When Implementing Insertion Sorting

#include <iostream> using namespace std; int main(){ int a[6] = {5, 2, 4, 6, 1, 3}; // create an array of size 6 int j, key = 0; for (int i = 1; i < 6; i++) { key = a[i]; j = i - 1; while ((j >= 0) && (a[j] > key)) { a[j + 1] = a[j]

Std :: unordered_map segmentation fault in constructor

I've been getting a strange segmentation fault when allocating objects that have unordered_map as an atribute, the debug seems to point to it happening somewhere over at hashtable.h when allocating the map. Anyone know why this happens? i reaches a v

Segmentation fault by changing `std :: string` by pointer

I have a std::string* as a member of my class, however when I try to set its value to anything in the class constructor, I'm getting segmentation fault (core dumped) Relevant code part: // class declaration: //... std::string* _sensorString; //... //

Linux Segmentation fault with std :: string :: iterator

I keep getting unusual segmentation faults inside libc.so.6 on a CentOS 6.4 64bit machine. This is the backtrace that gdb most often reports: 0x00007ffff60d9b3f in memcpy () from /lib64/libc.so.6 (gdb) backtrace #0 0x00007ffff60d9b3f in memcpy () fro

Segmentation fault with pointers in lists sorted in C

I have a function here that destroys an iterator acting on a sorted list. void SortedListDestroyTheIterator (SortedListIteratorPtr iter) { Node pt = NULL; Node prev = NULL; SortedListIteratorPtr walk; walk = malloc(sizeof(struct SortedListIterator)+1

c ++ std :: set insert causing a segmentation fault

All- I can't quite figure out why this piece of code is resulting in a segmentation fault... Any help would be great. #include <iostream> #include <set> using namespace std; class A{ public: int _x; A(int x){ _x = x; } }; bool fncomp(A a1, A a

Segmentation fault calling std :: map :: clear

I have been struggling with a segmentation fault for months, now I'm here to ask for help. The segmentation fault appears when I call the following function void foo(..., std::map<MyClass*, double> & x) { if ( !x.empty() ) x.clear(); ... } Class

C ++ segmentation fault in the std :: string function

I'm developing an algorithm based on Skyline queries using C++, using a RTree for storing my data. The algorithm works fine if I process up to 5 points then if I try 6 points it gives a segmentation fault. Using gdb I have discovered that the problem

Segmentation fault when sorting structure table in pointer

Possible Duplicate: How do I find a segfault in my C++ program? I am getting the segmentation fault while sorting the structure Here is my structure typedef struct { char *id; char *timestamp; char *name; char *text; }DATA; DATA *the_array = NULL; I

Segmentation fault and mysterious loop behavior

I am working on a homework assignment with a few specific requirements. There must be a class named TestScores that takes an array of scores as its argument. It throws an exception if any scores are negative or greater than 100. Finally, it must have

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK