Function callback problems

samad_lotia

Registered
I have written a sorting function for a resizeable array object in C++. The sorting function requires a pointer to a user-defined comparison function to be passed as a parameter. I noticed that sometimes the program crashes with a SIGBUS and sometimes it works fine without any changes to the program or environment whatsoever. I think what is going on is that the user-defined function's memory location changes, being totally normal, and when the function described by the pointer is executed, it finds out the pointer is not valid and crashes. Here are two example pointer values to the user-defined function:
0x3008 (This one works just fine)
0x6 (This doesn't work. That's quite obvious)
Anybody can help me out with this?
 
Hmm..I'm not sure how the function could be moving.

What's the function written in? If it's C++, is it static?

How's it declared and how are you passing it?

Wade
 
Here are the general statements that anybody can use:
typedef unsigned int uint;
typedef int (*SSortFunc) (const char *, const char *, uint count);
class SGenericArray;
void SGenericArray::sort(uint len, SSortFunc sort_func, int first = 0, int last = -1);
Here are the statements that is user-defined:
int intcmp(const int *a, const int *b, int len);
int intcmp(const int *a, const int *b, int len)
{
return (*a-*b);
}

{
SGenericArray x;
...
x.sort(4,(SSortFunc) intcmp,0,asize-1);

Does intcmp() need to be static?
Samad
 
Back
Top