Function pointer array initialization
2 posters
Page 1 of 1
Function pointer array initialization
I had the following function pointer definition in PBIT.h
typedef BOOL (*pfTests)(void);
and an array of function pointers in PBIT.c,
BOOL SRAM_BIT(void);
BOOL NVM_BIT(void);
BOOL PS_BIT(void);
BOOL ARINC_BIT(void);
pfTests pbits[] =
{
&SRAM_BIT,
&NVM_BIT,
&PS_BIT,
&ARINC_BIT
};
When building, the compiler gave the following warnings.
Source\BIT\PBIT.c:49: warning: missing braces around initializer
Source\BIT\PBIT.c:49: warning: (near initialization for 'pbits[0]')
Now when stepping to
pbits[i]();
the execution went to wrong place. Basically, it was because that the pbit[] was not initialized with the function addresses.
I used the working model from an ADSP-VisualDSP++ project, please advise why this does not work in Microchip MPLAB environment.
typedef BOOL (*pfTests)(void);
and an array of function pointers in PBIT.c,
BOOL SRAM_BIT(void);
BOOL NVM_BIT(void);
BOOL PS_BIT(void);
BOOL ARINC_BIT(void);
pfTests pbits[] =
{
&SRAM_BIT,
&NVM_BIT,
&PS_BIT,
&ARINC_BIT
};
When building, the compiler gave the following warnings.
Source\BIT\PBIT.c:49: warning: missing braces around initializer
Source\BIT\PBIT.c:49: warning: (near initialization for 'pbits[0]')
Now when stepping to
pbits[i]();
the execution went to wrong place. Basically, it was because that the pbit[] was not initialized with the function addresses.
I used the working model from an ADSP-VisualDSP++ project, please advise why this does not work in Microchip MPLAB environment.
GoldenMaple- Posts : 11
Join date : 2008-08-22
Re: Function pointer array initialization
Try to initialize them in an data initialization function.
Please post if that works.
Please post if that works.
Last edited by lzmind on Thu Jan 15, 2009 6:04 pm; edited 1 time in total
lzmind- Posts : 18
Join date : 2008-08-20
Re: Function pointer array initialization
Yes, your solution works. To demonstrate your solution, here is the code sample.
typedef BOOL (*pfTests)(void);
pfTests pbitFunctions[PBIT_FUNCTION_COUNT] = {'\0'};
//Initialize the function pointer array in run-time
pbitFunctions[0] = &pbitFunction1;
pbitFunctions[1] = &pbitFunction2;
pbitFunctions[2] = &pbitFunction3;
pbitFunctions[3] = &pbitFunction4;
for (pbitIndex = 0; pbitIndex < pbitCount; pbitIndex++)
{
pbitFunctions[pbitIndex]();
}
typedef BOOL (*pfTests)(void);
pfTests pbitFunctions[PBIT_FUNCTION_COUNT] = {'\0'};
//Initialize the function pointer array in run-time
pbitFunctions[0] = &pbitFunction1;
pbitFunctions[1] = &pbitFunction2;
pbitFunctions[2] = &pbitFunction3;
pbitFunctions[3] = &pbitFunction4;
for (pbitIndex = 0; pbitIndex < pbitCount; pbitIndex++)
{
pbitFunctions[pbitIndex]();
}
GoldenMaple- Posts : 11
Join date : 2008-08-22
Re: Function pointer array initialization
GoldenMaple,
Did you use the library default startup code or your own code? Can you double-check if the startup that was used have included the "__data_init" feature?
If ""__data_init" is included in your startup code, all initialized data variables (including your function pointer array) should be initialized properly. You can verify that by setting a brakpoint at main() entry.
However, if your startup code does not support "__data_init" feature, you'd have to initialize data explicitly.
Did you use the library default startup code or your own code? Can you double-check if the startup that was used have included the "__data_init" feature?
If ""__data_init" is included in your startup code, all initialized data variables (including your function pointer array) should be initialized properly. You can verify that by setting a brakpoint at main() entry.
However, if your startup code does not support "__data_init" feature, you'd have to initialize data explicitly.
lzmind- Posts : 18
Join date : 2008-08-20
Re: Function pointer array initialization
You are right, lzmind. I got the original startup code from someone else in the team, and I noticed that it was a simplified version that did not contain data initialization function. That was why my function pointer array did not get initialized.
Now I have added data initialization in the startup code, and I saw all data are initialized correctly. I removed the initialization of function pointer array from an init function, and my program works.
Thank you very much for your valuable help.
Now I have added data initialization in the startup code, and I saw all data are initialized correctly. I removed the initialization of function pointer array from an init function, and my program works.
Thank you very much for your valuable help.
GoldenMaple- Posts : 11
Join date : 2008-08-22
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|