Embedded Studio Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Function pointer array initialization

2 posters

Go down

Function pointer array initialization Empty Function pointer array initialization

Post  GoldenMaple Wed Jan 07, 2009 11:52 am

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.

GoldenMaple

Posts : 11
Join date : 2008-08-22

Back to top Go down

Function pointer array initialization Empty Re: Function pointer array initialization

Post  lzmind Wed Jan 07, 2009 4:49 pm

Try to initialize them in an data initialization function.
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

Back to top Go down

Function pointer array initialization Empty Re: Function pointer array initialization

Post  GoldenMaple Fri Jan 09, 2009 5:24 pm

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]();
}

GoldenMaple

Posts : 11
Join date : 2008-08-22

Back to top Go down

Function pointer array initialization Empty Re: Function pointer array initialization

Post  lzmind Thu Jan 15, 2009 6:02 pm

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.

lzmind

Posts : 18
Join date : 2008-08-20

Back to top Go down

Function pointer array initialization Empty Re: Function pointer array initialization

Post  GoldenMaple Tue Jan 20, 2009 2:55 pm

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.

GoldenMaple

Posts : 11
Join date : 2008-08-22

Back to top Go down

Function pointer array initialization Empty Re: Function pointer array initialization

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum