Casting Function Pointers

Discussion in 'C Programming' started by KKL, Jan 30, 2008.

  1. KKL

    KKL Guest

    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.
    KKL, Jan 30, 2008
    #1
    1. Advertising

  2. KKL

    Lew Pitcher Guest

    On Jan 30, 1:57 pm, KKL <> 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: 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]

    > 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.
    Lew Pitcher, Jan 30, 2008
    #2
    1. Advertising

  3. KKL

    KKL Guest

    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
    KKL, Jan 30, 2008
    #3
  4. KKL <> writes:
    > 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++.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 30, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dirk Vanhaute
    Replies:
    10
    Views:
    2,851
    Dirk Vanhaute
    Oct 22, 2004
  2. Alfonso Morra

    casting function pointers ?

    Alfonso Morra, Jul 27, 2005, in forum: C Programming
    Replies:
    4
    Views:
    565
    Barry Schwarz
    Jul 30, 2005
  3. Alfonso Morra

    Baffled by this ... (casting of function pointers)

    Alfonso Morra, Aug 26, 2005, in forum: C Programming
    Replies:
    7
    Views:
    366
    Denis Kasak
    Aug 26, 2005
  4. Alfonso Morra

    More questions about casting function pointers ..

    Alfonso Morra, Aug 31, 2005, in forum: C Programming
    Replies:
    3
    Views:
    348
    Alfonso Morra
    Aug 31, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    673
Loading...

Share This Page