The pointer is both constant and volatile

While reading, I came across this type of ad and the following line -

const volatile char *p=(const volatile char *) 0x30; 

The value of p changes only by external conditions.

I do not understand what external conditions. And also what is the practical use of this type of declaration?

+63
c volatile const
Jul 16 '15 at 13:54
source share
6 answers

const says that your program thread will not change what p points to. Any attempt to change the value after dereferencing the pointer will result in a compile-time error:

 *p = 'A'; // will not compile 

Please note that this is not a particularly strong contract; the value at 0x30 can still be changed using a non-constant anti-aliasing pointer, except for p :

 volatile char *q = 0x30; *q = 'A'; // will compile 

Another way to break this contract is to drop const from p :

 *(volatile char *) p = 'A'; // will compile 

However, volatile does not exclude changes that may be caused by another thread, kernel, asynchronous signal processor, or external device that has access to the same memory space. Thus, the compiler cannot accept the incorrect assumption that the value pointed to by p does not change and will load it from memory every time it refers:

 /* The character at 0x30 will be read on every iteration, even if the compiler has proven that the program itself doesn't modify the value at that address. */ while (*p) { ... } 

If the compiler had to erroneously optimize such a construction, it could generate instructions that load the value only once from memory, and then store it in the register. The register is essentially an independent copy, and any changes in the original location will not be reflected there, and, of course, this can cause some very unpleasant errors.

+59
Jul 16 '15 at 13:56
source share

Consider, for example, a hardware read-only register for your network card.

This may change outside the control of the program, so the compiler is not allowed to cache its value in the register or optimize it. Thus, volatile .

And this is read only, so you should not write to it. So const .

+23
Jul 16 '15 at 13:57
source share

First, let me give you an example from the C11 standard, chapter Β§6.7.3, type classifiers

Declared Object

extern const volatile int real_time_clock;

can be changed using hardware, but cannot be assigned, increased or decreased.

In addition, a related footnote (134),

A volatile declaration can be used to describe an object corresponding to a memory-mapped I / O port or to an object accessed by an asynchronous interrupt function. Actions on declared objects should not be "optimized" by the implementation or reordered, unless permitted by the rules for evaluating expressions.

This means that the value of the variable can be changed by hardware (via memory mapping), but cannot be changed "programmatically".

So, the advantage here is twofold,

  • The value that is ever used will be read from memory (caching is not allowed), giving you the last updated value (if updated).
  • The value cannot be changed (written down) intentionally or unintentionally by the program.
+22
Jul 16 '15 at 2:05
source share

We can use the article Introduction to the volatile keyword , which says:

A variable must be declared mutable when its value may change unexpectedly. In practice, only three types of variables can be changed:

  • Listed peripheral registers with memory
  • Global Variables Modified by Interrupt Service
  • Global variables in a multi-threaded application

and

Embedded systems contain real hardware, usually with sophisticated peripherals. These peripherals contain registers whose values ​​can asynchronously change the program flow. As a very simple example, consider the 8-bit status register at 0x1234. It is required that you poll the status register until it becomes non-zero. Nei and the wrong implementation are as follows:

 UINT1 * ptr = (UINT1 *) 0x1234; // Wait for register to become non-zero. while (*ptr == 0); // Do something else. 

This will almost certainly crash as soon as you turn on the optimizer, as the compiler will generate an assembly language that looks something like this:

 mov ptr, #0x1234 mov a, @ptr loop bz loop 

The constant says that your program will not change this variable, but, as indicated in the article, external sources can and volatile prevent its optimization.

+11
Jul 16 '15 at 2:00
source share

* const volatile char * p = (const volatile char) 0x30;
By understanding: the value of p changes only by external conditions.

It is clear that this type of variable can be considered as a logical lookup . It looks like a peephole in the door concept. The peephole allows you to view what is on the other side of the door, but does not allow you to change what is on the other side (const). However, the conditions outside the door can change at will (they are unstable). You can see what is happening, but you cannot change what is happening.

In the embedded system, for example, there are hardware registers designed to provide information about events occurring in the outside world. An optical encoder, for example, used to determine RPM, will set the value in the register. Each rotation determines the light from the LED and changes the value in the hardware register. This is what is meant by external conditions. On the other side of the image, that is, in the code (possibly in the PID loop), you can read this information for use in setting the loop, but you cannot change this value and do not want to. (const)

From the point of view of your software, this illustrates:

enter image description here

+7
Jul 16 '15 at
source share

const does not create a variable constant. It just forces the compiler to refuse write access. It is still possible to write to a variable (for example, using string pointers).

You can see const as protection against coding errors.

In this declaration, make sure that you do not arbitrarily write to p , telling the compiler not to optimize access (cache, disable (?), ...), because an external event can write to p .

+6
Jul 16 '15 at 14:02
source share



All Articles