Why can't I allocate an array of function pointers into FLASH?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why can't I allocate an array of function pointers into FLASH?

3,677 Views
wre
Contributor III
Hi, using CodWarrior 3.1 for HC(S)08, I am trying to get an array of function pointers allocated into FLASH (.rodata).  Instead they are allocated into RAM (.data) and initialization code loads the array.
 
Here is what I am doing:
 
const void (*sapfnIPHandlers [])(void) =
{
  func1,
  func2,
  func3,
  ...
  funcn
}
 
void func1 (void)
{
  ...
}
 
etc.
 
I have tried the -Cc compiler option with no change in the output.
 
This code is simply a way of parsing some commands to get to the right function.  What am I doing wrong (or what is the compiler doing wrong)?
 
Thanks
Labels (1)
0 Kudos
Reply
3 Replies

906 Views
CompilerGuru
NXP Employee
NXP Employee
The reason why the array ends up in RAM is a common error in C programming.
For pointers (including function pointers), qualifiers have to be after the *, hence this array:
>const void (*sapfnIPHandlers [2])(void);
if not constant. You can assign to it, try

>void test(void) { sapfnIPHandlers[0]= fun; }

The correct version is:
>void (*const sapfnIPHandlers [2])(void)= {fun0, fun1};

Well this works, but it is a bit hard to read in my point of view.
So for functions, I would use a typedef instead:

>typedef void (*FunctionType)(void);
>const FunctionType sapfnIPHandlers[2]= {fun0, fun1};

Bye
Daniel
0 Kudos
Reply

906 Views
wre
Contributor III

Many thanks for both responses.  The first one got me going without using RAM and the second simplifies things...although I will have to study it a bit more to see what it is actually doing...

typedef void (*FunctionType)(void);

OK, I see that it matches an example from K&R such that it is creating the type FunctionType which is a pointer to a function with no arguments and returns nothing.

Thanks

Message Edited by wre on 04-18-200611:54 AM

0 Kudos
Reply

906 Views
tequila
Contributor I
Function pointers are normally stored in RAM, here is a workaround:
 
const unsigned int pFunction[]={(unsigned int)&SafePoint,(unsigned int)&BlackButton};
 
and you call it this way:
 
 ((void (*)(void))pFunction[0])();
 
Cheers:smileyhappy:
0 Kudos
Reply