2

What are the real-time applications of const volatile type qualifier? In which scenario would one use this. I know the applications of volatile keyword & const qualifiers, but I don't understand the usage of const volatile together. Please share yours thoughts.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • http://www.embedded.com/electronics-blogs/barr-code/4236917/Combining-C-s-volatile-and-const-keywords – Clifford Feb 26 '16 at 20:54
  • Neither the`volatile` nor the `const` qualifier are related to real-time. – too honest for this site Feb 27 '16 at 12:40
  • @Olaf - perhaps he meant "real-world"? – Clifford Feb 28 '16 at 09:09
  • There are three scenarios regarding the use of these qualifiers together; these are not really addressed in the "duplicate" (which is not really a duplicate, even if the answer is applicable). – Clifford Feb 28 '16 at 09:15
  • @Clifford: Speculation. There is sooo much confusion about what RT means. The term is even missused in commercials. We are no clairvoyants. – too honest for this site Feb 28 '16 at 14:08
  • @Olaf : Indeed but at least "real-world" would make some kind of sense, but given the tag "embedded", he may equally be conflating "real-time" with "multi-threaded" (and in an RTOS). The purpose of my "speculation" was to encourage the author to improve the question - addressing it to you was entirely rhetorical. It is disappointing when the original authors do not engage - as if they are not really interested in the answer to their own question. – Clifford Feb 28 '16 at 16:14
  • @Clifford: I prefer taking such questions literally. I'd say as asked, the question is too broad. There is a plethora of RT applications using such variables. Talking about embedded: a short look into an MCU header likely shows already some examples. – too honest for this site Feb 28 '16 at 16:22

2 Answers2

3

const and volatile can be combined in three ways to different and useful effect. Examples:

  1. To declare a constant address of a hardware register:

    uint8_t volatile* const p_led_reg = (uint8_t *) 0x80000;
    
  2. To declare a read-only inter-processor shared-memory, where the other processor is the writer:

    int const volatile comm_flag;
    
    uint8_t const volatile comm_buffer[BUFFER_SIZE];
    
  3. To declare a read-only hardware register:

    uint8_t const volatile* const p_latch_reg = (uint8_t *) 0x10000000;
    

Note that type qualifiers in each of these cases are:

  • volatile* const - Constant address to variable volatile data.
  • const volatile - Read-only volatile data.
  • const volatile* const - Constant address to read-only volatile data.

A complete description of these usages is provided in Michael Barr's Embedded.com article Combining C's volatile and const keywords

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

The C standard (ISO/IEC 9899:2011 §6.7.3 Type qualifiers) gives an example:

EXAMPLE 1 An object declared

extern const volatile int real_time_clock;

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

This tells the C compiler that although the program can't modify the real time clock, the real time clock can change and therefore must be treated with circumspection when optimizing code that references it.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278