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 foo1()
below, but I am curious about the method in foo2()
.
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;
}
See the dissassembly (compiled with gcc 4.7.2 and -O3):
foo1:
movl 0xdeadbeef, %eax
movl %eax, -0x4(%rsp)
ret
foo2:
movl 0xdeadbeef, %eax
ret
The method in foo2()
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.