I am a C ++ programmer for the most part, but just for fun I tried to do some general programming in C. In particular, I implemented a general sorting algorithm. Signature of my function
int sort(void *data,
size_t num_elems,
size_t elem_size,
int (*cmp)(const void*, const void*))
When I compared this to qsort()the standard library, I noticed that, unlike my function, qsort()it has no return value. Since swap elements are always required to sort an array, temporary size storage is required for implementation elem_size. Since C has no templates, it is elem_sizenot known at compile time, so the temporary storage must be allocated dynamically, which can lead to failure. In this case, qsort()it was not possible to sort the array, but it also could not report an error, so there is no way to find out if the array is sorted upon return.
Did I miss something?
source
share