Talk:c/algorithm/qsort
[edit] GNU and CRT versions
Could we mention in the Note section that C11 qsort_s function differs from the one in Microsoft's CRT ?
C11 Annex K: errno_t qsort_s( void *ptr, rsize_t count, rsize_t size, int (*comp)(const void *, const void *, void *), void *context );
void qsort_s (void *base, size_t num, size_t width, int (*compare )(void *, const void *, const void *), void * context);
There is also GNU qsort_r on Linux:
void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);
And a different one in FreeBSD's:
void qsort_r(void *base, size_t nmemb, size_t size, void *thunk, int (*compar)(void *, const void *, const void *));
The return type and constraint handler notwithstanding, GNU's version is compatible with C11's. Sadly, both Microsoft and FreeBSD use a different parameters order, and even worse, so does their comparison function, which means the incompatibility cannot be fixed by a macro or wrapper function.
This causes a lot of confusion, so I think an addendum mentioning this would be helpful in the article.
Babbage (talk) 15:32, 23 August 2022 (PDT)
- I would vote F your proposal. Can be useful. --Space Mission (talk) 16:23, 23 August 2022 (PDT)
- W.r.t. MS's
_s
functions. From https://open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm: -
Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. For example, it doesn't provide the set_constraint_handler_s function but instead defines a _invalid_parameter_handler _set_invalid_parameter_handler(_invalid_parameter_handler) function with similar behavior but a slightly different and incompatible signature. It also doesn't define the abort_handler_s and ignore_handler_s functions, the memset_s function (which isn't part of the TR), or the RSIZE_MAX macro. The Microsoft implementation also doesn't treat overlapping source and destination sequences as runtime-constraint violations and instead has undefined behavior in such cases
- Not sure if there would be a good place for a general note like this? But yeah, it's best assumed that MS doesn't implement annex K --Ybab321 (talk) 01:36, 24 August 2022 (PDT)
- W.r.t. MS's
[edit] Problematic sentence
The article states (in the Notes):
"Until qsort_s, users of qsort often used global variables to pass additional context to the comparison function."
Among the major C standard libraries, only the Windows CRT supports the Annex K/bounds-checking interfaces. And Microsoft's qsort_s
does not conform to the C standard. This makes this sentence very misleading: there is still no standard context-aware qsort() in C.
Babbage (talk) 14:28, 21 January 2024 (PST)