Liste des Groupes | Revenir à cl c |
On 6/20/24 17:39, Ben Bacarisse wrote:Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:>
Ben Bacarisse <ben@bsb.me.uk> writes:
[...]On a C language point, I don't think the standard says anything about>
sorting with non-order functions like the one above. Is an
implementation of qsort permitted to misbehave (for example by not
terminating) when the comparison function does not implement a proper
order relation?
N1570 7.22.5p4 (applies to bsearch and qsort):
"""
When the same objects (consisting of size bytes, irrespective of
their current positions in the array) are passed more than once to
the comparison function, the results shall be consistent with one
another. That is, for qsort they shall define a total ordering on
the array, and for bsearch the same object shall always compare
the same way with the key.
"""
>
That's a "shall" outside a constraint, so violating it results in
undefined behavior.
I think it should be clearer. What the "that is" phrase seems to
clarify in no way implies a total order, merely that the repeated
comparisons or the same elements are consistent with one another.
I don't think they were talking only about multiple comparisons of the
same value producing the same result. I think that they were also
talking about consistency between the result on different pairs of
values. In order to sort a, b, and c, the results of comp(a,b),
comp(b,c) and comp(a,c) need to be consistent with each other, or
there's no well-defined sort order. "total order" is merely a more
specific and precise way of specifying that consistency requirement, but
it is a consistency requirement, and therefore the most plausible kind
of consistency they could have been referring to with that comment.
Les messages affichés proviennent d'usenet.