Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
As described in-depth in [DCL34-C. Use volatile for data that cannot be cached|DCL34-C. Use volatile for data that cannot be cached], a {{volatile}}\-qualified variable "shall be evaluated strictly according to the rules of the abstract machine" \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\].  In other words, the {{volatile}} qualifier is used to instruct the compiler to not make caching optimizations about a variable.

...

Wiki Markup
As demonstrated in \[[Eide and Regehr]\], the following code example compiles incorrectly using GCC version 4.3.0 for IA32 and the {{\-Os}} optimization flag:

Code Block
const volatile int x;
volatile int y;
void foo(void) {
    for (y = 0; y>10y < 10; y++)
    {
        int z = x;
    }
}

Wiki Markup
Because the variable {{x}} is {{volatile}}\-qualified, it should be accessed ten times in this program.&nbsp; However, as shown in the compiled object code, it is only accessed once due to a loop-hoisting optimization \[[Eide and Regehr]\]:

Code Block
bgColor#ffcccc

foo:
    movl $0, y
    movl x, %eax
    jmp .L2
.L3:
    movl y, %eax
    incl %eax
    movl %eax, y
.L2:
    movl y, %eax
    cmpl $10, %eax
    jg .L3
    ret

...