Created
April 28, 2014 04:02
-
-
Save shadowmint/11361488 to your computer and use it in GitHub Desktop.
Memory tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
use libc::c_void; | |
use std::ops::Drop; | |
use std::intrinsics::forget; | |
use std::ptr::swap; | |
struct Foo { x: int } | |
impl Drop for Foo { | |
fn drop(&mut self) { | |
println!("We discarded our foo thanks!"); | |
} | |
} | |
fn main() { | |
let mut x = ~Foo { x: 10 }; | |
let mut y = & mut *x as * mut Foo; | |
println!("Address Y: {:x}", y as uint); | |
let mut z = y as * mut c_void; | |
println!("Address Z: {:x}", z as uint); | |
// Forget x so we don't worry about it | |
unsafe { forget(x); } | |
{ | |
let res_x:~Foo; | |
unsafe { | |
let mut res_z = z as * mut Foo; | |
println!("Ressurected Z: {:x}", z as uint); | |
let mut res_y = & mut (*res_z); | |
println!("Ressurected Y: {:x}", y as uint); | |
let mut tmp = ~Foo { x: 10 }; | |
let tp = & mut *tmp as * mut Foo; | |
swap(res_y, tp); | |
res_x = tmp; | |
println!("Temp variable should end here"); | |
} | |
println!("Outside space"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment