Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 227
ameyCU Avatar asked Jul 16 '15 13:07

ameyCU


People also ask

What is pointer to constant volatile data?

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application.

Can we use constant and volatile together?

Yes a C++ variable be both const and volatile. It is used in situations like a read-only hardware register, or an output of another thread. Volatile means it may be changed by something external to the current thread and Const means that you do not write to it (in that program that is using the const declaration).

What is the effect of the variable declaring as both const and volatile?

We use 'const' keyword for a variable when we don't want to the program to change it. Whereas when we declare a variable 'const volatile' we are telling the program not to change it and the compiler that this variable can be changed unexpectedly from input coming from the outside world.

Can pointer be volatile?

Yes, a pointer can be volatile if the variable that it points to can change unexpectedly even though how this might happen is not evident from the code. An example is an object that can be modified by something that is external to the controlling thread and that the compiler should not optimize.

Are pointers const or volatile?

Pointers declared as volatile, or as a mixture of const and volatile, obey the same rules. Pointers to const objects are often used in function declarations as follows:

How do you declare a pointer as volatile in C++?

To declare the value of the pointer — that is, the actual address stored in the pointer — as const or volatile, use a declaration of the form: char * const pchc; char * volatile pchv; The C++ language prevents assignments that would allow modification of an object or pointer declared as const.

Can we create a pointer to a constant in C?

We can create a pointer to a constant in C, which means that the pointer would point to a constant variable (created using const ). We can also create a constant pointer to a constant in C, which means that neither the value of the pointer nor the value of the variable pointed to by the pointer would change. How const Pointer Works in C?

What is the difference between const and volatile in C++?

The const and volatile keywords change how pointers are treated. The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.


1 Answers

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.

like image 58
Blagovest Buyukliev Avatar answered Sep 20 '22 06:09

Blagovest Buyukliev