Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Posted Mar 22, 2022 16:14 UTC (Tue) by jlombera (guest, #155698)In reply to: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul by tialaramex
Parent article: Beingessner: Rust's Unsafe Pointer Types Need An Overhaul
Again, "volatile" is to ask the *compiler* not to optimize access to memory in the generated binary code.
>You can take a look at a toy example with Godbolt, and see for yourself what the compiler actually tells your CPU to do
Sure, feel free to play with this (very contrived) example in godbolt.org (sorry for the formatting, I couldn't fine a way to make this work as a plain text comment):
```
void f(volatile int *x_p) {
while (!*x_p)
;
}
void g(int *x_p) {
while (!*x_p)
;
}
```
Feel free to play with different compilers, optimization levels, even different archs. You'll see that in every case, in the loop in f() *x_p is read from memory in every iteration, whereas for g(), different kind of optimizations are performed.
