I have a single buffer, and several pointers into it. I want to sort the pointers based upon the bytes in the buffer they point at.
qsort() and stl::sort() can be given custom comparision functions. For example, if the buffer was zero-terminated I could use strcmp:
int my_strcmp(const void* a,const void* b) { const char* const one = *(const char**)a, const two = *(const char**)b; return ::strcmp(one,two); }
however, if the buffer is not zero-terminated, I have to use memcmp() which requires a length parameter.
Is there a tidy, efficient way to get the length of the buffer into my comparision function without a global variable?
Is there a reason you can’t null-terminate your buffers?
If not, since you’re using C++ you can write your own function object: