Let us declare const object as volatile and compile code
with optimization option. Although we compile code with optimization option,
value of const object will change, because variable is declared as volatile
that means don’t do any optimization.
/* Compile
code with optimization option */
#include
<stdio.h>
intmain(void)
{
Const
volatile int local = 10;
int*ptr
= (int*) &local;
printf("Initial
value of local : %d \n", local);
*ptr
= 100;
printf("Modified
value of local: %d \n", local);
return0;
}
|
Output:
$ gcc -O3
volatile.c -o volatile –save-temp
$
./volatile
Initial value of local :
10
Modified value of local:
100
If we do not use volatile qualifier, the following problems may arise
1) Code may not work as expected when optimization is turned on.
2) Code may not work as expected when interrupts are enabled and used
1) Code may not work as expected when optimization is turned on.
2) Code may not work as expected when interrupts are enabled and used
No comments:
Post a Comment