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.
 
L

Lew Pitcher

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;

OK, I thought that I was good at reading C declarations, but this one
escapes me

funcPtr is a (void() (void)) pointer. I have no idea what a void()
modifier to a pointer does. Is this even legal C code?

errStatus = rtosTaskCreate( taskName, &TaskProperties, &TaskId);

.........

}

Module: SomeClass.cpp

void someClass::taskLoop( int taskArgument)
{

Oops.... now you've slipped into another language. The above (and the
elided code below) is not C, and we (here in comp.lang.C) don't have
any way to predict the behaviour of the code. It's as if you wrote
some LISP code here, and asked us to figure it out.

[snip]
 
K

KKL

Hi Lew,

Let me make the task easy by sticking to C Langauge

Delete the module SomeClass.cpp

Replace rest of the .Cpp with C, and delete all "SomeOtherClass::" &
"AnotherClass".

Now we have all C functions and question remain the same.

Regards
Kishore
 
K

Keith Thompson

KKL said:
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?
[code snipped]

The code you posted is C++, not C.

In C, you can legally cast any pointer-to-function type to any other
pointer-to-function type and back again, and the result is guaranteed
to compare equal to the original pointer value. Attempting to use a
pointer function of a type other than the actual type of the
pointed-to function invokes undefined behavior.

The behavior of casting a pointer to an object or incomplete type
(e.g., void*) to a function pointer, or vice versa, is undefined.
Some implementations may support this as an extension, but other
implementations may, for example, store more information in a function
pointer than in a void*, making meaningful conversions difficult or
impossible.

The rules in C++ may differ. Try asking in comp.lang.c++.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top