tags:

views:

95

answers:

1

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?
+5  A: 

Is the algorithm used always a quicksort or is it compiler-implementation-dependent?

It is implementation dependent.

Would you recommend using this function or is there a real benefit to templates?

C does not have templates. If you need a generic sorting function in C, then qsort is 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.

Are there any things I should watch out for to avoid security problems/segfaults?

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.

James McNellis
Thank you for the answer!
Delan Azabani