PDA

View Full Version : structs, pointer, static callback function



vonCZ
2nd June 2008, 14:41
I have a static callback function like so:



void Coin3dScene::flythruCB(void *data, SoSensor *s)
{
MyFlythruData *locData = (MyFlythruData *)data;
//etc...
}

The "data" I pass to the function is a struct that references variables I need to access in the callback:



typedef struct
{
float *flyTimeLen;
bool *flyLoop;
} MyFlythruData;

set-up like so:



MyFlythruData *myflydata = new MyFlyData;
myflydata->flyTimeLen = &FlyTime;
myflydata->flyLoop = &FlyLoop;


My question: is it necessary to use pointers in the struct as I've done? The callback function is called 50 times/second; by passing pointers to variables rather than the variable itself, the function should operate more smoothly with less overhead, right?

But on the other hand, since the callback receives a pointer to data rather than just "data"... it's unnecesary to use pointers for the variables inside the struct... if I don't otherwise need to. right?

wysota
2nd June 2008, 15:19
No, you can allocate the data on stack. Using pointers is just an overhead here and also a possible memory leak. Remember that using pointers requires the memory to be allocated and for that to happen your process needs to be preemptied so that the OS can give it some memory.

vonCZ
18th June 2008, 16:18
thanks for the help Wysota.

Another question: I've lotsa data in this struct now, that I'm passing to the static callback function. I also need to include a pointer in the struct to a member function... so I can call that function from within the callback. I've looked around on the web but haven't found a solution that fits my particular situation: so far I get various compile errors. I'm trying various things like this:

//in header:



class Coin3dWindow : public SoQtExaminerViewer {

private:

void fly0_stop(void);

typedef struct {

void (*flyStop)(void);
// void (*Coin3dWindow::flyStop)();

} MyFlyData;

MyFlyData *myflydata0;
.
.
.
}


// then in a Coin3dWindow member function I do



myflydata0 = new MyFlyData;
myflydata0->flyStop = &fly0_stop;

this is where I get the compile error:

error C2276: '&' : illegal operation on bound member function expression

if I drop the '&'

myflydata0->flyStop = fly0_stop;

i get:

error C3867: 'Model::fly0_stop' : function call missing argument list; use '&Model::fly0_stop' to create a pointer to a member
error C2440: '=' : cannot convert from 'void (_thiscall Model::*)(void)' to 'void (_cdecl *)(void)'

so if I change it to:


myflydata0->flyStop = &Model::fly0_stop;

I just get that last error message (the C2440, but not the C3867 obviously). Anyhow, this is the way I've seen it explained on the web... but I'm not sure why it won't compile. Any ideas?

BTW, fly0_stop(void) is currently empty except for a single qDebug("hit");.

jacek
20th June 2008, 12:53
error C2440: '=' : cannot convert from 'void (_thiscall Model::*)(void)' to 'void (_cdecl *)(void)'
That flyStop field was declared as a pointer to a function that has no parameter and doesn't return anything. You can't make it point to a method of Model class, unless you change its type to void (Model::*flyStop)(void).