ameyCU ameyCU - 1 year ago 75
C Question

Pointer declared as constant as well as volatile

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

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

The value of p is changed by external conditions only

I don't get what are the external conditions . And also what is practical use of this type of declaration?

Answer Source

The const says that the flow of your program isn't going to modify what is pointed to by p. Any attempt to modify the value after dereferencing the pointer will result in a compile-time error:

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

Note that this isn't a particularly strong contract; the value at location 0x30 can still be changed through an aliasing non-const pointer, other than p:

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

Another way to break this contract is by casting away the const from p:

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

The volatile, however, doesn't exclude any modifications which could be caused by another thread, the kernel, an asynchronous signal handler or an external device which has access to the same memory space. This way the compiler cannot make the wrong assumption that the value pointed to by p doesn't change and will load it from memory every time it is referenced:

 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 was to erroneously optimise such a construct, it could emit instructions which load the value only once from memory and then keep it in a register. The register is essentially an independent copy and any changes to the original location will not reflect there, and, needless to say, this can cause some very nasty bugs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download