C & C++ Programming: Data Modifier
Volatile
The compilation system tries to reduce code size and execution time on all machines,
by optimizing code. It is programmer responsibility to inform compilation system
that certain code (or variable) should not be optimized. Many programmers do not
understand when to use volatile to inform compilation system that certain code should
not be optimized or what it does. Although (no doubt) their program work, they do
not exploit the full power of the language.
The volatile keyword can be used to state that a variable may be changed by hardware,
the kernel, another thread etc.
For example, the volatile keyword may prevent unsafe compiler optimisations for
memory-mapped input/output
The volatile keyword tells the compiler that the value of the variable may change
at any time, so it can't optimize in such a way that repeated references to the
value of the variable don't access the actual variable's storage for each reference
(common subexpression reduction, moving the value to a register, and other common
optimizations do this).
It is generally used on objects (variables) that are shared between threads or programs,
changed by signal handlers, or in device registers.
Without the volatile keyword, the compiler is free to convert expressions like:
Code:
a = c + c + c;
into
Code:
a = 3 * c;
////////////////////////////////////////////////////////////////////////////////////////////
// In this example, the code sets the value stored in foo to 0. It then starts to poll that
// value repeatedly until it changes to 255:
static int foo;
void bar(void) {
foo = 0;
while (foo != 255)
;
}
// An optimizing compiler will notice that no other code can possibly change the value stored
// in foo, and will assume that it will remain equal to 0 at all times. The compiler will
// therefore replace the function body with an infinite loop similar to this:
void bar_optimized(void) {
foo = 0;
while (true)
;
}
// However, foo might represent a location that can be changed by other elements of the
// computer system at any time, such as a hardware register of a device connected to the CPU.
// The above code would never detect such a change; without the volatile keyword, the
// compiler assumes that the current program is the only part of the system that could
// change the value (which is by far the most common situation).
// To prevent the compiler from optimizing code as above, the volatile keyword is used:
static volatile int foo;
void bar (void) {
foo = 0;
while (foo != 255)
;
}
// With this modification the loop condition will not be optimized away,
// and the system will detect the change when it occurs.
/////////////////////////////////////////////////////////////////////////////////////////////
// A common use of volatile in modern C code is with pointers to locations.
// In the example
volatile unsigned long * sysclock = (unsigned long *)SYSCLOCK;
unsigned long then;
unsigned long now;
int i;
then = *sysclock;
for (i = 0; i < 10000; ++i) {
// do nothing in the loop -- just see how long it takes to do 10000 iterations
}
now = *sysclock;
printf("the loop took %uld clock cycles\n", now - then);
The compiler wouldn't know that the value pointed to by sysclock might change, since
no function calls are being made, between the reading of its value into the variable
"then" and the later reading of its value into the variable "now" without the volatile
keyword. The code that the compiler sees certainly never changes it. Not all compilers
will optimize this sort of thing, but many will.
Const
The use of pointers and the const keyword is quite subtle:
- const int *p is a pointer to a const int
- int const *p is also a pointer to a const int
- int *const p is a const pointer to an int
- const int *const p is a const pointer to a const int
1 int main(void) {
2 int i = 42;
3 int j = 28;
4
5 const int *pc = &i; //Also: "int const *pc"
6 *pc = 41; //Wrong
7 pc = &j;
8
9 int *const cp = &i;
10 *cp = 41;
11 cp = &j; //Wrong
12
13 const int *const cpc = &i;
14 *cpc = 41; //Wrong
15 cpc = &j; //Wrong
16 return 0;
17 }
Mutable