Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Address-of-a-variable/Rust/address-of-a-variable-1.rust
Normal file
11
Task/Address-of-a-variable/Rust/address-of-a-variable-1.rust
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let v1 = vec![vec![1,2,3]; 10];
|
||||
println!("Original address: {:p}", &v1);
|
||||
let mut v2;
|
||||
// Override rust protections on reading from uninitialized memory
|
||||
unsafe {v2 = mem::uninitialized();}
|
||||
let addr = &mut v2 as *mut _;
|
||||
|
||||
// ptr::write() though it takes v1 by value, v1s destructor is not run when it goes out of
|
||||
// scope, which is good since then we'd have a vector of free'd vectors
|
||||
unsafe {ptr::write(addr, v1)}
|
||||
println!("New address: {:p}", &v2);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let var = 1;
|
||||
println!("address of var: {:p}", &var);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
let address: usize = 0x7ffc8f303130;
|
||||
unsafe {
|
||||
let val = *(address as *const usize);
|
||||
println!("value at {}: {:?}", address, val);
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
unsafe {
|
||||
*(0x7ffc8f303130 as *mut usize) = 1;
|
||||
// Note that this invokes undefined behavior if 0x7ffc8f303130 is uninitialized. In that case, std::ptr::write should be used.
|
||||
std::ptr::write(0x7ffc8f303130 as *mut usize, 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue