I have a struct with two pointers to functions and some data. The functions are definition-wise the same, however they should perform different tasks (which is why they have different names in the original source). If I use the first function, all goes well, but if I use the second functions, I get a segfault – even if I pass the same pointer to both functions.
The pointers are neither NULL nor garbage, or else both would fail – but somehow only the latter gets segfaulted. Why is that?
I have the following code:
typedef void (*funcOneDef)(void*);
typedef void (*funcTwoDef)(void*);
typedef struct structImpl* structPt;
struct structImpl {
void *data;
funcOneDef funcOne;
funcTwoDef funcTwo;
};
structPt create(void *data, funcOneDef funcOne, funcTwoDef funcTwo)
{
structPt test = malloc(sizeof(test));
test->data = data;
test->funcOne = funcOne;
test->funcTwo = funcTwo;
return test;
}
void execFuncOne(structPt test)
{
test->funcOne(test->data); //works!
}
void execFuncTwo(structPt test)
{
test->funcTwo(test->data); //segfault!
}
PS: No need to test this, because somehow this works, but my original source – which is basically the same – doesn’t?
Shouldn’t the line
structPt test = malloc(sizeof(test));bestructPt test = malloc(sizeof(structImpl));?