Skip to content

Instantly share code, notes, and snippets.

@sguzman
Created July 1, 2018 00:28
Show Gist options
  • Save sguzman/34a0b6936049fbdc917853b89fe49ce9 to your computer and use it in GitHub Desktop.
Save sguzman/34a0b6936049fbdc917853b89fe49ce9 to your computer and use it in GitHub Desktop.
A simple example involving raw pointers, padding, structs, byte addressing and stuff
pub fn main() {
struct Lel {
ch: char,
num: u64
}
struct Yip {
lel: Lel,
b: bool
}
println!("sizeof(char) -> {}", std::mem::size_of::<char>());
println!("sizeof(u64) -> {}", std::mem::size_of::<u64>());
println!("sizeof(bool) -> {}", std::mem::size_of::<bool>());
println!("sizeof(Lel) -> {}", std::mem::size_of::<Lel>());
println!("sizeof(Yip) -> {}", std::mem::size_of::<Yip>());
let le = Lel {
ch: 0x01 as char,
num: 0xffffffffffffffff
};
println!("{}", le.ch);
println!("{}", le.num);
let le_ptr = &le as *const Lel;
let byte_le_ptr = le_ptr as *const u8;
const SIZE: usize = std::mem::size_of::<Lel>();
for offset in 0..SIZE {
let offset = offset as u64;
let new_addr = byte_le_ptr as u64;
let new_addr = new_addr + offset;
let new_byte_addr = new_addr as *const u8;
let new_byte = unsafe {
*new_byte_addr
};
println!("address({}) -> byte({})", new_addr, new_byte)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment