calling qsort with a pointer to a c++ function -
i have found book states if want use function c standard library takes function pointer argument (for example qsort), function of want pass function pointer needs c function , therefore declared extern "c".
e.g.
extern "c" { int foo(void const* a, void const* b) {...} } ... qsort(some_array, some_num, some_size, &foo);
i not surprised if wrong information, - i'm not sure, so: correct?
a lot depends on whether you're interested in practical answer compiler you're using right now, or whether care theoretical answer covers possible conforming implementations of c++. in theory it's necessary. in reality, can without it.
the real question whether compiler uses different calling convention calling global c++ function when calling c function. most compilers use same calling convention either way, call work without extern "c"
declaration.
the standard doesn't guarantee though, in theory there compiler used different calling conventions two. @ least offhand, don't know of compiler that, given number of compilers around, wouldn't surprise me terribly if there 1 don't know about.
otoh, raise question: if you're using c++, why using qsort
@ all? in c++, std::sort
preferable -- easier use , faster well.
Comments
Post a Comment