Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question: when does simply taking the address of something
- // invoke undefined behavior?
- // (run in miri)
- // Answer:
- fn main() { unsafe {
- let mut a = 0;
- let b = &mut a as *mut _;
- let c = &mut *b;
- let addr = &a as *const _ as usize;
- *c = 2;
- } }
- // The answer in C is "Never! That would be crazy."
- //
- // But according to the "Stacked Borrows" memory model, whenever a variable
- // is borrowed exclusively - even indirect borrowing through a raw pointer
- // like this case - that borrow only remains valid if the address of the
- // variable isn't taken.
- //
- // That's kinda like saying that the address ceases to exist. And that's
- // *really* strange. And how does it generalize to multiple threads?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement