trouble adding "const" to array of function pointers

20 Sep 2010

I have an array of function pointers that I'd like to ensure stays in program memory, rather than consume RAM.  However, when I add the "const" keyword, I get a compiler error.  Here is the code as it exists without the compiler error:

PmReturn_t (* usr_nat_fxn_table[])(pPmFrame_t *) =
{
    nat_placeholder_func,
    nat_01_mbed___init__,
    nat_02_mbed_read_u16,
    /* ... many more funcs ... */
};
Here is how I insert "const":
PmReturn_t (* const usr_nat_fxn_table[])(pPmFrame_t *) =
{
    nat_placeholder_func,
    nat_01_mbed___init__,
    nat_02_mbed_read_u16,
    /* ... many more funcs ... */
};
The compiler error is "Undefined symbol usr_nat_fxn_table"
I've published the working code here pymite_mbed_r586

The place to insert const is at src/platform/mbed/main_nat.c:1569

The table is used in src/vm/interp.c:32 and 2032

Does my C-like understanding of const not apply to a C++ compiler?

!!Dean
20 Sep 2010

I found a nice explanation here. I added the following to pm.h:

typedef PmReturn_t (* NATFNPTR) (pPmFrame_t *);
extern NATFNPTR const std_nat_fxn_table[];
extern NATFNPTR const usr_nat_fxn_table[];
Changed main_nat.c:

NATFNPTR const usr_nat_fxn_table[] =
and pmstdlib_nat.c:

NATFNPTR const std_nat_fxn_table[] =
removed extern definitions from interp.c, and everything compiled!

NB: in your pm.h you have wrong nesting of #ifdefs for __PM_H__ and __cplusplus.

20 Sep 2010

In contrast, the following program with a simple array of function pointers with a const declaration compiles and works just fine: http://mbed.org/users/dwhall/programs/ArrayOfFxnPtrs/latest

20 Sep 2010

GCC handles the "PmReturn_t (* const usr_nat_fxn_table[])(pPmFrame_t *) =" declaration just fine.  But I imagine using a typedef will be more compatible across different compilers.  Thank you Igor.

(I posted my other reply before I refreshed and saw your reply)

!!Dean

20 Sep 2010

Your example works in one file but not if you define table in one and use it in another.

20 Sep 2010

Actually, I take that back. I was able to make it link by declaring the definition of the table "extern". I guess mbed compiler used default hidden visibility, so you need to explicitly mark stuff you want exported as such.