P99

◆ qsort_s

#define qsort_s (   B,
  N,
  S,
  CMP,
  CTX 
)

A generic sorting routine.

This an implementation of the qsort variant as it is specified in Annex K of the C11 standard. Its particularity is that it additionally takes a "context" argument, which allows to program comparison functions more widely, without refering to global variables. Its prototype if it were not implemented as a type generic macro, would be:

errno_t qsort_s(void *base,
rsize_t nmemb,
rsize_t size,
int (*compar)(const void *x, const void *y, void *context),
void *context);

Because this is a type generic macro, the implementation can take care of the "real" base type of base, in particular about alignment. If it really only would receive a void* it would have to make the worst assumptions about that and always use memcpy to copy elements around. If it has more type information it can use more efficient means for that. So casting the first argument to void* wouldn't be a good idea.

Definition at line 496 of file p99_qsort.h.

rsize_t
size_t rsize_t
Definition: p99_libc.h:164
qsort_s
#define qsort_s(B, N, S, CMP, CTX)
A generic sorting routine.
Definition: p99_qsort.h:496
errno_t
int errno_t
Definition: p99_type.h:38