Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. // Question: when does simply taking the address of something
  2. // invoke undefined behavior?
  3.  
  4. // (run in miri)
  5. // Answer:
  6.  
  7. fn main() { unsafe {
  8. let mut a = 0;
  9. let b = &mut a as *mut _;
  10. let c = &mut *b;
  11. let addr = &a as *const _ as usize;
  12. *c = 2;
  13. } }
  14.  
  15. // The answer in C is "Never! That would be crazy."
  16. //
  17. // But according to the "Stacked Borrows" memory model, whenever a variable
  18. // is borrowed exclusively - even indirect borrowing through a raw pointer
  19. // like this case - that borrow only remains valid if the address of the
  20. // variable isn't taken.
  21. //
  22. // That's kinda like saying that the address ceases to exist. And that's
  23. // *really* strange. And how does it generalize to multiple threads?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement