qsort(), bsearch(), and rude comparison functions

Paul Eggert eggert at ata.twinsun.com
Sat Oct 6 11:34:43 AEST 1990


Suppose I pass to qsort() a comparison function that isn't a total order.
May qsort() dump core?

For example, NN 6.4.10 passes the function order_subj_date() to qsort().  This
function is not a total order: there are values A,B,C such that A<B, B<C, and
C<A according to this function.  (This bug is fixed in NN 6.4.11.)  SunOS 4.1's
qsort() dumps core sometimes in this situation.  It seems to me that a
conforming qsort() should be allowed to dump core here, but I don't see where
the ANSI C standard permits this.

While we're on the subject, what happens if the comparison function has side
effects, benign or otherwise?  How are the sequence points defined here?  For
example, suppose I qsort() an N-item array using a comparison function that
increments a global counter starting at zero; must the counter be at least N-1
when qsort() returns?

Similar questions apply to bsearch().



More information about the Comp.std.c mailing list