Suresh bangaram
Suresh bangaram

Reputation: 47

What are the real-time applications of `const volatile` type qualifier?

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.

Upvotes: 2

Views: 1425

Answers (2)

Clifford
Clifford

Reputation: 93476

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

Upvotes: 3

Jonathan Leffler
Jonathan Leffler

Reputation: 754060

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.

Upvotes: 1

Related Questions