I have a function that goes through an array of function pointers and calls each of these functions in order.
What happens if the contents of the array of functions pointers is changed during this process? Can the function calls be considered atomic enough to be sure that nothing unexpected happens, or must care be taken not to change the function pointers while for example the push on the stack is taking place?
Some example (pseudoish) code below. init() is run once at startup, and callFunctions() is run periodically, say once each second. Then changeFunctions() comes along and changes the content of functionPtrArray[]. This may happen at any point of time, since the code is run in different processes a OS like environment.
void (*functionPtrArray[3]) ( void );
void init( void )
{
functionPtrArray[0] = function1;
functionPtrArray[1] = function2;
functionPtrArray[2] = function3;
}
void callFunctions( void )
{
for ( i = 0; i < 3; i++ )
{
*functionPtrArray[i]();
}
}
void changeFunctions( void )
{
functionPtrArray[0] = newFunction1;
functionPtrArray[1] = newFunction2;
functionPtrArray[2] = newFunction3;
}