Casting Function Pointers

K

KKL

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: taskAbstraction.c
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 someClass::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.
 
A

Alf P. Steinbach

* KKL:
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: taskAbstraction.c
int taskCreate(char* taskName, void* funcPtr, int

C++ does not support casting between data pointers (such as void*) and
function pointers.

In the other direction, C++ supports OOP and generic programming.

Either of those suffice to create a customizable and type safe task
abstraction. E.g., you can pass in a pointer to an object of abstract
class with a virtual 'run' member function. But better, use an existing
task abstraction library, e.g. Boost threads if your tasks are threads.


Cheers, & hth.,

- Alf
 
E

Erik Wikström

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: taskAbstraction.c
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 someClass::initialize( void )
{

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

}

A function pointer and a member-function pointer is not the same thing,
this will not work.
Module: SomeOtherClass.cpp

int myTaskEntryReturnsInt( void )
{

}

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

}

myTaskEntryReturnsInt() returns an int, not void, this will not work.
Module: AnotherClass.cpp
void myTaskEntryTakesInt( int TaskArg )
{

}

myTaskEntryTakesInt() takes an argument, this will not work.
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.

If you know the type of the function why bother casting it to void? Why
not use a correctly typed function pointer to begin with?
 
K

KKL

Hi Erik,

The point is that how this program works. It is an existing code that
is working and thus there is no question of writing the code in some
other way or code not working. Probably you can omit CPP classes and
consider only c functions used for Task 2 and Task 3. But the code is
working for Task1, Task2 and Task3 although I am not sure how and
whether it is working without any problems.

Regards
Kishore
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top