33

I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:

register volatile int T=10;

Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)

const volatile int T=10;

The program itself can not modify T, but T can be modified frow somewhere outside the code.

static volatile int T=10;

If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.

Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?

Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133

1 Answers1

37
register volatile int T=10;

volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, While register is a hint to the compiler that T will be heavily used. If address of T is taken, the hint is simply ignored by the compiler. Note that register is deprecated but still used.

Practical Usage:

I have never used it never felt the need for it and can't really think of any right now.


const volatile int T=10;

const qualifier means that the T cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile still means the same as in case 1. The compiler cannot optimize or reorder access to T.

Practical Usage:

  • Accessing shared memory in read-only mode.
  • Accessing hardware registers in read-only mode.

static volatile int T=10;

static storage qualifier gives T static storage duration (C++11 §3.7) and internal linkage, while volatile still governs the optimization and reordering.

Practical Usage:

  • Same as volatile except that you need the object to have static storage duration and to be inaccessible from other translation units.
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    For the second one, are you sure you're talking about const-volatile *objects*, or const-volatile *references* (or pointers to const-volatile objects)? Because it doesn't seem like a const-volatile object could map to hardware... – user541686 Apr 28 '13 at 06:03
  • 1
    @Mehrdad: I was referring to, pointer to const-volatile objects something like: `unsigned char const volatile *hd_addr;` – Alok Save Apr 28 '13 at 06:07
  • 1
    Yeah, your example shows `const volatile int T=10;` which is different. :) – user541686 Apr 28 '13 at 06:09
  • 2
    @Mehrdad: ah okay, It is the example OP quoted. I was trying to use it as a placeholder for explanation and I added the examples later on so kinda missed it. Anyhow now that you pointed it out, its there in comments to see for those who deserve to see :) – Alok Save Apr 28 '13 at 06:11
  • 1
    @Mehrdad: a linker script can bind a symbol to a particular place in a memory map, so as long as the `const volatile int` is not also `static`, it could potentially map to hardware. Linker tricks like this are best avoided because they're not very intuitive, but I've seen them before in low-level embedded stuff. – cooperised Aug 03 '17 at 15:42
  • @Gary99 That link concerns `volatile`, which clearly does have a variety of real-world applications. The context of your quotation was a comment on the usefulness of the specific combination of `register volatile`. – cooperised Aug 03 '17 at 15:44