Operating System - HP-UX
1832145 Members
2869 Online
110038 Solutions
New Discussion

Re: improper behaviour with -O option

 
Satya_6
Frequent Advisor

improper behaviour with -O option

Hi,

I have a c code something like this

void RegisterFunc(
int (*cb)(char *)
void *client_data
)
{
callBackList[i].cb = cb;
callBackList[i].cdata = client_data;
}

void InvokeCallback()
{
callBackList[i].cb((char *)callBackList[i].cdata)
}

int my_func(char *name)
{
if(strcmp(name, "TEST"))
return -1;

/* do the processing */
}

void foo()
{
RegisterFunc(my_func, "TEST");
}

The argument name coming to 'my_func' is NULL when I use -O option for cc. It works fine when I remove the optimisation option.

Please suggest a way out

TIA
satya
1 REPLY 1
Mike Stroyan
Honored Contributor

Re: improper behaviour with -O option

If "callBackList[i].cb" is some generic function pointer type, then you should cast it before calling through it.
*((int (*)(char *)) callBackList[i].cb)((char *)callBackList[i].cdata)

That will tell the compiler what the parameter list should look like. Without that it may assume that the parameter to the call is a (32-bit) integer. That would damage a 64-bit char *.

If that isn't the problem then I would suggest creating a small test case. If it fails you will have an example. If it works then you will have a difference to ponder while looking at your original code.