I really like the qsort function in C. It’s so easy to use and allows me to procrastinate learning C++ template types. I have a few questions about this:
- Is the algorithm used always a quicksort or is it compiler-implementation-dependent?
- Would you recommend using this function or is there a real benefit to templates?
- Are there any things I should watch out for to avoid security problems/segfaults?
It is implementation dependent.
C does not have templates. If you need a generic sorting function in C, then
qsortis a good choice.If you are going to use C++, then you should use
std::sort, which is much easier to use correctly and gives type safety.If you use the function improperly (for example, if you pass it incorrect parameters or if your comparison function has bugs in it), then your program might well crash (or might otherwise perform incorrectly). Of course, this isn’t specific to
qsort; this is true for anything used in a program.