Segmentation fault custom sort with a struct

I am using a custom sort function with a deliberate bug. I want to know why is it failing.
Code: https://pastebin.com/d7mrJQtT
Test case: https://pastebin.com/Qp0yp5nm
You don't have a complete description of less than.
Nothing jumps out at me as being incorrect. I assume line 20 is the deliberate bug.

Since the closing "}" is missing from what you pasted, I don't know if this is the complete program or if there is something after line 33 that could be causing problems. Could the seg fault be coming after what you have posted?
If the comparison function doesn't fulfill the requirements there are no guarantees about what will happen.
http://en.cppreference.com/w/cpp/concept/Compare
Last edited on
I don't think there is a problem with the comparison function. I took what was posted, added my own printout statements at the end of main, and ran it. There were no segmentation faults. (After I fixed the "deliberate bug", the output even looked nice and consistent).

As I said before, I suspect the error is in the remainder of the code that was not posted.
doug4 wrote:
I don't think there is a problem with the comparison function.

It does not satisfy the requirements for strict weak ordering.

Just imagine what would happen if you declared two variables as follows.

1
2
3
//          i  a  b
test t1 = {-1, 1, 0};
test t2 = {-1, 2, 0};

cmp(t1, t2) would return false which means t1 should be positioned after t2,
but cmp(t2, t1) would also return false which means t2 should be positioned after t1.

This is a contradiction!
Last edited on
> there are no guarantees about what will happen.
>> There were no segmentation faults.
no checks are done, no behaviour is expected
¿do you still don't understand what undefined means?

> cmp(t1, t2) would return false(...)
> but cmp(t2, t1) would also return false(...)
> This is a contradiction!
No, in that case they are equivalent.
the problem is when cmp(t1, t2) and cmp(t2, t1) returns true.
ne555, you're right. I made a mistake.

A better example would have been ...

1
2
3
//          i  a  b
test t1 = {-1, 1, 2};
test t2 = {-1, 0, 2};
Last edited on
Topic archived. No new replies allowed.