Bruno Haible <[EMAIL PROTECTED]> writes:

> you are now assuming an in-place sorting algorithm. If a libc were
> to implement qsort by use of mergesort (and temporary storage), such
> a sorting algorithm would pass you two pointers to hol_entrys in
> different array.

A minor quibble: the C standard (both C89 and C99) does not allow such
an implementation for qsort; it requires that the arguments to the
comparison function be bona fide pointers into the
originally-specified array.

> To make this portable, I see two options:
>   - Sort an array of 'hol_entry*' instead of an array of 'hol_entry';
>     then you can be sure the hol_entrys stay in place. Or
>   - Add a field 'array_index' to the hol_entry struct. Fill these fields
>     right before calling qsort.

I agree with this suggestion.  Even though the standard requires that
qsort be in-place, it does not require that qsort must behave by
swapping elements in the naive way.  Also, it's possible that the C
library is buggy in this area (glibc used to have such a bug), as the
point is fairly obscure.  So a fix is necessary, and the two options
you mention both should work.


Reply via email to