fork download
  1. use std::mem;
  2.  
  3. fn main() {
  4. const N: usize = 32;
  5.  
  6. let mut addrs = Vec::new();
  7.  
  8. for _ in 0..20 {
  9. // Prevent optimization by taking raw pointers and using volatile reads
  10. let arr: [u8; N] = [0; N];
  11. let ptr = &arr as *const _ as usize;
  12. unsafe { std::ptr::read_volatile(&ptr); }
  13.  
  14. addrs.push(ptr);
  15. }
  16.  
  17. for (i, addr) in addrs.iter().enumerate() {
  18. println!(
  19. "{:2}: {:#x} aligned_to_usize = {}",
  20. i,
  21. addr,
  22. addr % mem::align_of::<usize>() == 0
  23. );
  24. }
  25. }
  26.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
 0: 0x7ffd26caadc0   aligned_to_usize = true
 1: 0x7ffd26caadc0   aligned_to_usize = true
 2: 0x7ffd26caadc0   aligned_to_usize = true
 3: 0x7ffd26caadc0   aligned_to_usize = true
 4: 0x7ffd26caadc0   aligned_to_usize = true
 5: 0x7ffd26caadc0   aligned_to_usize = true
 6: 0x7ffd26caadc0   aligned_to_usize = true
 7: 0x7ffd26caadc0   aligned_to_usize = true
 8: 0x7ffd26caadc0   aligned_to_usize = true
 9: 0x7ffd26caadc0   aligned_to_usize = true
10: 0x7ffd26caadc0   aligned_to_usize = true
11: 0x7ffd26caadc0   aligned_to_usize = true
12: 0x7ffd26caadc0   aligned_to_usize = true
13: 0x7ffd26caadc0   aligned_to_usize = true
14: 0x7ffd26caadc0   aligned_to_usize = true
15: 0x7ffd26caadc0   aligned_to_usize = true
16: 0x7ffd26caadc0   aligned_to_usize = true
17: 0x7ffd26caadc0   aligned_to_usize = true
18: 0x7ffd26caadc0   aligned_to_usize = true
19: 0x7ffd26caadc0   aligned_to_usize = true