Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why did std::sort use this simple strict weak ordering compare still crash?

Here is my code:

#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
void test()
{
    vector<vector<int>> info = {{0,0,999}, {0,1,1000}, {0,2,1001},
                {0,3,1002}, {0,4,1003}, {0,5,1004},
                {0,6,1005}, {0,7,1006}, {1,0,1000},
                {1,1,999}, {1,2,1000}, {1,3,1001},
                {1,4,1002}, {1,5,1003}, {1,6,1004},
                {1,7,1005}, {2,0,1001}, {2,1,1000},
                {2,2,999}, {2,3,1000}, {2,4,1001},
                {2,5,1002}, {2,6,1003}, {2,7,1004},
                {3,0,1002}, {3,1,1001}, {3,2,1000},
                {3,3,999}, {3,4,1000}, {3,5,1001},
                {3,6,1002}, {3,7,1003}, {4,0,1003},
                {4,1,1002}, {4,2,1001}, {4,3,1000},
                {4,4,999}, {4,5,1000}, {4,6,1001},
                {4,7,1002}, {5,0,1004}, {5,1,1003},
                {5,2,1002}, {5,3,1001}, {5,4,1000},
                {5,5,999}, {5,6,1000}, {5,7,1001},
                {6,0,1005}, {6,1,1004}, {6,2,1003},
                {6,3,1002}, {6,4,1001}, {6,5,1000},
                {6,6,999}, {6,7,1000}, {7,0,1006},
                {7,1,1005}, {7,2,1004}, {7,3,1003},
                {7,4,1002},{7,5,1001}};  
    auto cmp=[](const auto& a,const auto& b){
      if(a[2]<b[2])return true;
      if(a[0]<b[0])return true;
      return a[1]<b[1];       
 };

 sort(begin(info),end(info),cmp);

}
int main()
{
  test();
}

I use g++ 13.2 compile under Ubuntu 24.04 LTS(Linux 6.8.0-39-generic) build and run the code crashes on sort, very strange:

zzhao@zzhao-System-Version:~$ g++  -std=c++20 z3.cpp

zzhao@zzhao-System-Version:~$ ./a.out

Segmentation fault (core dumped)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your comparator does not respect strict-weak ordering.

Take for example:
a = {2,0,1}, b = {1,0,2}

It will return true for a<b (due to the first if in the comparator), but also true for b<a (due to the second if in it).

Since this does not satisfy the requirement of a comparator for std::sort, it causes undefined behavior.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading