As we all know, the following code will be optimized by all sensible compilers:
const int a = 3;
const int b = 42;
if (a == b)
{
do_something();
}
If the two constants are the same, the if
is omitted and the function always called, if they are different, the whole block is omitted.
However, there can be situations where it is important to NOT optimize this code, as the program itself can be modified before it is run. E.g. the binary sits in flash memory and can be accessed by an external program, and of course the adresses of the two const
s are fixed. (makes sense in embedded).
I'm thinking of using #pragma
, but that is not part of the C standard.
Another way would be to use const volatile
. Is that guaranteed to work on all standard-compliant compilers?