Re: Casting function pointers



On Jan 31, 12:18 pm, Habib Bouaziz-Viallet <ha...@xxxxxxxxxxxxx>
wrote:
Le Wed, 30 Jan 2008 10:46:26 -0800, KKL a écrit:

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;

(void ()(void)* ) funcPtr does not make any sense.

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.

Is your code compiles ? No offense, If so i wish you good luck (and
therefore to those who work with you)

--
HBV

Hi Habib,

I am a new use to the group and did not know that putting code
snippets that do not compile is such a serious offense even if you are
discussing here some logical issues here and not syntactical ones.

Ok, so I accept my offense that the code snippets have syntactical
issues and that is because I did not simply copy it from my C source
files but just wrote it in hurry to put up an example for my point of
discussion which was "IS TYPE CASTING FUNCTION POINTERS FROM void (*)
(int) TO (void *) AND BACK TO void (*)(void) AND THEN USING IT TO
CALL FUNCTIONS IS A VALID AND SAFE OPERATION OR NOT".

And regarding your comment about Good luck to those that work with me,
may be you don't know that if making logical mistakes costs you much
more than making syntactical mistakes and those mistakes are mostly
done by people who stresses more on syntax rather than the actual
thing which is LOGIC. So a good luck is required more to people who
miss the whole point and and try hard to prove their knowledge
stressing on the syntax (since that itself is hard thing for them).

So, now I think you know what it means to take offense.

Regards

.



Relevant Pages

  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • ToolTips in a View and TTN_NEEDTEXT
    ... extern int g_minWorkPeriod; ... void CDayView::CreateAllFonts ... void CDayView::DrawDayLog(CDC* pDC) ... BOOL CDayView::TimeToY ...
    (microsoft.public.vc.mfc)