|
|
|
|
|
by haberman
4803 days ago
|
|
There be dragons. The following program prints 10 on gcc 4.6.3, x86-64: #include <stdio.h>
#include <stdint.h>
void f(uint32_t *x, uint16_t *y) {
*x = 5;
printf("%d\n", *y);
}
int main() {
uint32_t x = 10;
f(&x, (uint16_t*)&x);
}
|
|
The reason this example is fundamentally different from my example is because mine doesn't create two objects that point to the same memory. In such situations, memory barriers are necessary. Also, your program won't work on different platforms due to endianness.