This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problems with init of a function pointer

Hello

I've a problem when initializing a global function pointer. Depending on where the function pointer is created, the adress of the funciton pointer is 0 (=wrong). There are no warnings in the compiler!
Compiler: c166 v4.23
Tool: uVision2 v2.31

Example:

typedef UINT16 (*pModul_Para)(UINT16, UINT16 *, void *, UINT16);

//prototype of the function
UINT16 Controller_Para(UINT16 ParamNr, UINT16 *pElementNr, void *pValue, UINT16 Action);

//create and init the function pointer
pModul_Para FunctionPtr123 = &Controller_Para;

//Function definition
UINT16 Controller_Para(UINT16 ParamNr, UINT16 *pElementNr, void *pValue, UINT16 Action)
{ ...
}

//create and init a second function pointer
pModul_Para FunctionPtr55 = &Controller_Para;

--> Result: FunctionPtr123 would be 0, FunctionPtr55 can be the address of Controller_Para.

Any idea? Thanks!

Parents Reply Children
  • Hi Tamir,

    Tried quite a while to find that thread, but no luck; will re-try with other keywords later.

  • To Tamir, the mentioned thread:

    http://www.keil.com/forum/17103/


    Per Westermark
    24-Jun-2010 13:20 GMT

    There is a rule that "A pointer to void may be converted to or from a pointer to any incomplete or object type." So you may typecast between a void pointer and some other data type.

    But when an architectire have __far, __near, __code, __data, __base etc, it isn't automagically possible to store all pointers as a char pointer (or as a void pointer).

    This means that void* isn't an automagic solution that can handle anything for real platforms even if the standard somehow implies this. It may be compatible with a function pointer. But what if you have a memory model where code is large and data is small - so a function pointer may need a larger size than a char* pointer. For old 16-bit x86 code, char* is byte-aligned byt in "compact" memory model it is 16 bit large while a function pointer is 32-bit large.


    Hans-Bernhardn Broeker
    24-Jun-2010 17:49 GMT

    The C programming languages supports Harvard architecures, and that means code and data pointers are assumed to be fundamentally different things. There is no allowed way to convert from one to the other.