Std :: sort throws Segmentation Fault C ++
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 ++
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
Palindrome of string gets a segmentation fault
Vector :: size and Segmentation fault
Segmentation Fault Error During Matrix Basic Operation
Segmentation Fault When Implementing Insertion Sorting
Std :: unordered_map segmentation fault in constructor
Segmentation fault by changing `std :: string` by pointer
Linux Segmentation fault with std :: string :: iterator
Segmentation fault with pointers in lists sorted in C
c ++ std :: set insert causing a segmentation fault
Segmentation fault calling std :: map :: clear
C ++ segmentation fault in the std :: string function
Segmentation fault when sorting structure table in pointer
Segmentation fault and mysterious loop behavior
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK