Re: Casting function pointers



On Jan 30, 7:46 pm, KKL <kishore.lut...@xxxxxxxxx> wrote:
I wonder how casting with function pointers work, how it "should be"
handled and how it "should not be" handled. Can anyone tell whether
the below code should work or not?

Module: Task Service Abstraction Layer
int taskCreate(char* taskName, void* funcPtr, int
taskPriority,..........)
{
myTaskStructure TaskProperties;
int TaskId, errStatus;

............
//Casting void* funcPtr to function pointer that returns void and
accepts no arguments
TaskProperties.entryPtr = (void ()(void)* ) funcPtr;
errStatus = rtosTaskCreate( taskName, &TaskProperties, &TaskId);

.........

}

Module: SomeClass.cpp

void someClass::taskLoop( int taskArgument)
{
.......

}

void somClass::initialize( void )
{

taskCreate( "Task1", (void* )&taskLoop, 25,.....);

}

Module: SomeOtherClass.cpp

int myTaskEntryReturnsInt( void )
{

}

SomeOtherClass::initialize( void )
{
taskCreate( "Task2", (void* )&myTaskEntryReturnsInt, 30,.....);

}

Module: AnotherClass.cpp
void myTaskEntryTakesInt( int TaskArg )
{

}

AnotherClass::initialize( void )
{
taskCreate( "Task3", (void* )&myTaskEntryTakesInt, 40,.....);

}

My question is whether these kind of casting for function pointers is
valid. If valid should it work with a predictable behavior or the
behavior is undefined.

Why ?

The example makes no sense. If your task mechanism doesn't support
tasks returning/accepting any kind of value, then why bother declaring
your task entries as functions that do so ?

Just declare your task entries as

void myTaskEntry( void )

And use:

int taskCreate(char* taskName, void (*funcPtr)( void), ...

No casts are required that way, and the compiler will be able to
verify pointer types for you.
.



Relevant Pages