28

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 consts 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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
vsz
  • 4,811
  • 7
  • 41
  • 78
  • 1
    If you assume that the binary could be changed than anything you do could be changed as well. Any solution to the comparison problem could be invalidated by changing the binary. – Matteo Apr 23 '13 at 07:23
  • @Matteo: of course, only the values of the `const`s are supposed to be changed externally – vsz Apr 23 '13 at 07:28
  • Relying on the variables being at a fixed location and for the code to be unoptimized is asking for trouble. If you need to read from flash then just explicitly do that. – James Apr 23 '13 at 07:48
  • @James : that does not always work. If you have a big array that does not fit twice in the memory, you cannot just copy it fully to temporary storage. Handling it in parts can become complicated and large. – vsz Apr 23 '13 at 07:51

2 Answers2

23

Yes, const volatile int a = 3; does exactly what you want, and is standards compliant from C89 onwards (See section 3.5.3 of C89).

This excellent answer describes const volatile in detail, for use in a situation similar to yours.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • 1
    Thanks for the link to the standard and to the other other question. I was planning on augmenting my answer to include the standard reference but you beat me to it. Excellent answer: – Nik Bougalis Apr 23 '13 at 07:37
6

The const volatile should do the trick because the comparison requires a "read" of the two volatile variables - and the standard guarantees the reads will be treated as having observable side-effects.

Therefore they cannot just be optimized away.

I must ask, what problem are you trying to solve that requires this solution? Explaining the problem may allow us to offer suggestions and solutions you may not have considered which will not require this kind of hoop-jumping.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • 3
    For example, an embedded program has some settings. A special and small "admin" program can modify those settings. Both programs are in the same eeprom. To handle the settings easily as if they were variables, you can just declare them as constants and assign them fixed addresses (for example, in the mapping file). Without an operating system and `printf`/`scanf` this is the simplest way to store settings to non-volatile memory. – vsz Apr 23 '13 at 07:48