2
    println!("{}", std::mem::size_of::<*mut i32>());
    println!("{}", std::mem::size_of::<*mut [u8]>());

I thought the print results should be the same, after all, pointers are essentially memory addresses. And the size of memory addresses should be the same.

However, when I run the above code, it turns out the size of i32 pointer is 8 while the size of array pointer is 16. How is that coming? Maybe some inner mechanism of array is affecting the result, could somebody explain this to me? Thanks.

LunarEclipse
  • 661
  • 1
  • 4
  • 14
  • 4
    `*mut [u8]` doesn't point to an array, it points to what would otherwise be an unsized slice of memory, so the "fat" pointer includes the size. You'd obtain a pointer to an array using `*mut [u8; 32]` or something like that, and it'd be of expected size. – user4815162342 Mar 12 '21 at 07:56

0 Answers0