I am using C++ with the arm-none-eabi-gcc compiler and I am wondering why the new compiler version (10.3.1) decides to optimize out a function call which modifies a volatile variable while the old compiler version (7.3.1) does not.
Following are the relevant code parts extracted:
InterruptHandler.h:
class InterruptHandler : public iInterruptHandler {
public:
void registerInterruptCallback(iInterruptCallback *callback, int16_t interrupt);
private:
static volatile iInterruptCallback *callbackTable[callbackTableSize];
InterruptHandler.cpp:
volatile iInterruptCallback *InterruptHandler::callbackTable[callbackTableSize] = {nullptr};
void InterruptHandler::registerInterruptCallback(iInterruptCallback *callback, int16_t interrupt) {
callbackTable[interrupt] = callback;
}
App.h:
class App {
private:
InterruptHandler interruptHandler;
App.cpp:
App::App() : interruptHandler() {
interruptHandler.registerInterruptCallback(&taskingController, TASK_HANDLER_IRQ);
}
In total the function "registerInterruptCallback" is called 4 times, of which one function call is optimmized out. The pointers in "callbackTable" are then used in the InterruptCallback to call the relevant function for the interrupt, which is then, of course, not working if there is no pointer available due to optimization.
My assumption was that the compiler is not allowed to optimize the function call because the variable is declared as volatile.
Is it correct behavior of the compiler and if yes, why?
Additional information:
I tried to give the function "registerInterruptCallback" the attribute for no optimization __attribute__((optimize("O0")))
which is then working, but I thought this should be solvable with standard C++ means.