This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: Virtual Function

Tool/software: TI C/C++ Compiler

I am writing simple 'C' code for an MSP430.  How do I create a virtual function? I have a CRC routine that get called from several points in my program and in an interrupt routine.

  • Why does the function need to be virtual? Why can't it be just a plain function?
  • If you are in the process of executing a function and an interrupt occurs that uses the same function, the second call inside the interrupt can corrupt variables used by the first call. I could disable the problem interrupt but that causes another issue. I can not afford the latency the disable might cause when getting to the interrupt. I was trying not to be forced to inline the function code in the interrupt because I am short of flash and did not want to use the extra flash. Even if you can make it work with more optimization the adverse effects on maintainability when debugging are sometimes simply not worth it. Thanks.
  • What you want is a function that is re-entrant with respect to interrupts. The best way to do this is to make sure the function does not use any global state, such as global variables, during execution of the function. Try to have all of the inputs be parameters to the function. If there are too many, put them in a struct or class. If you absolutely cannot avoid using global state, you're going to have to do something more complicated. Consider moving the function call out of the interrupt handler and adding it to a queue, and check the queue periodically. There are many possible solutions depending on the nature of your program.