Difference between const & const volatile

Solution 1:

An object marked as const volatile will not be permitted to be changed by the code (an error will be raised due to the const qualifier) - at least through that particular name/pointer.

The volatile part of the qualifier means that the compiler cannot optimize or reorder access to the object.

In an embedded system, this is typically used to access hardware registers that can be read and are updated by the hardware, but make no sense to write to (or might be an error to write to).

An example might be the status register for a serial port. Various bits will indicate if a character is waiting to be read or if the transmit register is ready to accept a new character (ie., - it's empty). Each read of this status register could result in a different value depending on what else has occurred in the serial port hardware.

It makes no sense to write to the status register (depending on the particular hardware spec), but you need to make sure that each read of the register results in an actual read of the hardware - using a cached value from a previous read won't tell you about changes in the hardware state.

A quick example:

unsigned int const volatile *status_reg; // assume these are assigned to point to the 
unsigned char const volatile *recv_reg;  //   correct hardware addresses


#define UART_CHAR_READY 0x00000001

int get_next_char()
{
    while ((*status_reg & UART_CHAR_READY) == 0) {
        // do nothing but spin
    }

    return *recv_reg;
}

If these pointers were not marked as being volatile, a couple problems might occur:

  • the while loop test might read the status register only once, since the compiler could assume that whatever it pointed to would never change (there's nothing in the while loop test or loop itself that could change it). If you entered the function when there was no character waiting in UART hardware, you might end up in an infinite loop that never stopped even when a character was received.
  • the read of the receive register could be moved by the compiler to before the while loop - again because there's nothing in the function that indicates that *recv_reg is changed by the loop, there's no reason it can't be read before entering the loop.

The volatile qualifiers ensures that these optimizations are not performed by the compiler.

Solution 2:

  • volatile will tell the compiler not to optimise code related the variable, usually when we know it can be changed from "outside", e.g. by another thread.
  • const will tell the compiler that it is forbidden for the program to modify the variable's value.
  • const volatile is a very special thing you'll probably see used exactly 0 times in your life (tm). As is to be expected, it means that the program cannot modify the variable's value, but the value can be modified from the outside, thus no optimisations will be performed on the variable.

Solution 3:

It is not because the variable is const that it may not have changed between two sequence points.

Constness is a promise you make not to change the value, not that the value won't be changed.

Solution 4:

In C, const and volatile are type qualifiers and these two are independent.

Basically, const means that the value isn’t modifiable by the program.

And volatile means that the value is subject to sudden change (possibly from outside the program).

In fact, the C Standard gives an example of a valid declaration which is both const and volatile. The example is:

extern const volatile int real_time_clock;

where real_time_clock may be modifiable by hardware, but cannot be assigned to, incremented, or decremented.

So we should already treat const and volatile separately. These type qualifiers can be applied to struct, union, enum and typedef as well.

Solution 5:

I've needed to use this in an embedded application where some configuration variables are located in an area of flash memory that can be updated by a bootloader. These config variables are 'constant' during runtime, but without the volatile qualifier the compiler would optimise something like this...

cantx.id = 0x10<<24 | CANID<<12 | 0;

...by precomputing the constant value and using an immediate assembly instruction, or loading the constant from a nearby location, so that any updates to the original CANID value in the config flash area would be ignored. CANID has to be const volatile.