title: Force read of volatile variable
body: <p>An embedded project I'm working on requires reading a specific location in memory, but the value of that memory location is not needed. Currently I am reading the volatile variable into a dummy variable as in <code>foo1()</code> below, but I am curious about the method in <code>foo2()</code>.</p>
  
  <pre><code>void foo1(void) {
      volatile uint32_t *a = (volatile uint32_t *)0xdeadbeef;
      volatile uint32_t discard = *a;
  }
  void foo2(void) {
      volatile uint32_t *a = (volatile uint32_t *)0xdeadbeef;
      *a;
  }
  </code></pre>
  
  <p>See the dissassembly (compiled with gcc 4.7.2 and -O3):</p>
  
  <pre><code>  foo1:
  movl      0xdeadbeef, %eax
  movl      %eax, -0x4(%rsp)
  ret
    foo2:
  movl      0xdeadbeef, %eax
  ret
  </code></pre>
  
  <p>The method in <code>foo2()</code> seems to work, but I want to know if it is guaranteed to work and isn't a side effect of the compiler version and optimizations I am using.</p>