Ben Bacarisse said:
Bull in a China Blue Shop <
[email protected]> writes:
It's in most modern versions of the GNU C library (apparently since
version 2.8) but it's not documented yet.
Argh! I decided to write the missing man page and in so doing I found
the following mess:
The GNU version has prototype
void qsort_r(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *),
void *data);
I.e. the data pointer is last and gets passed as the third argument to
the comparison function.
The BSD version is
void qsort_r(void *base, size_t nmemb, size_t size, void *data,
int (*compar)(void *, const void *, const void *));
with the data pointer as the fourth argument which gets passed as the
first argument to the comparison function.
Microsoft decided to do this:
void qsort_s(void *base, size_t nmemb, size_t size,
int (*compare)(void *, const void *, const void *),
void *data);
using the argument order of the GNU version and the comparison function
of the BSD version.
Annex K of the new C standard defines an option set of library extension
based on Microsoft's supposedly "safer" functions. Here we find (edited
for consistency):
errno_t qsort_s(void *base, rsize_t nmemb, rsize_t size,
int (*compar)(const void *, const void *, void *),
void *data);
which follows the GNU version except for the name and the return type!
I don't know why the online documentation for Microsoft systems does not
used this version.
I think qsort_r is probably
close to being reliably available on *nix type systems but I don't know
about Windows C libraries.
So, no. There is no widely available qsort function that takes a data
pointer. If the documentation is right, you can't even use the
quasi-standard qsort_s with Microsoft's C library.
It's a shame that we are left with this mess. Annex K is optional, and
is an all-or-nothing set of extensions (I may have misunderstood that)
so qsort_s is likely to languish alongside all the other _s functions.
qsort_r is functionally better that qsort -- the change is not a
mythical safety advantage.
<snip>