[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Undefined qsort behavior



Katie Lucas wrote:

In this context, it most definitely means:

  The order of two or more elements for which the 'compar' function
  returns zero - are undefined RELATIVE TO EACH OTHER.

...and not "completely undefined"
Did I say that?

I said "don't use undefined behaviour that looks like it might do
something you want because it might change without you being told".
I'm sure you understand that.

My concern is that the general tone of this thread was a discrediting
of qsort.   It absolutely ALWAYS and RELIABLY sorts things into order
as advertised - even when there are items that compare equal.

The original poster was clearly confused about that.

The man page may be poorly worded - but any system whose qsort does not
return a sorted result (eg 1 3 2 2 instead of 1 2 2 3) would result in a
non-functioning machine in pretty short order
Oh I dunno. Solaris got away with a qsort that would crash when the
array had just three things in it for quite a while...
Yikes!

Yes. Really.
But a hypothetical problem that made it fail when two items were identical
would be very serious to MANY shell scripts.  The standard 'sort' program
uses 'qsort' - and the mechanism:

    sort filename | uniq

...is frequently used for the specific purpose of eliminating multiple
occurrances of some line in the file.  If sort crapped out when there
were multiple identical lines, MANY scripts would immediately fail - you'd
know it pretty soon.

libtool does this (for example) - and you'd find it pretty hard to have
a working modern UNIX system without libtool.
---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1@airmail.net>    WorkEmail: <sjbaker@link.com>
HomePage : http://web2.airmail.net/sjbaker1
Projects : http://plib.sf.net    http://tuxaqfh.sf.net
           http://tuxkart.sf.net http://prettypoly.sf.net