offset是偏移元素个数,不是字节数!
fn main(){let student_a = Student{id:20240001,name:"张三娃".into(),class_id:3,age:14,grade:1};let student_b = Student{id:20240002,name:"李四牛".into(),class_id:3,age:15,grade:1};let student_c = Student{id:20240003,name:"王二狗".into(),class_id:3,age:14,grade:1};let student_d = Student{id:20240004,name:"吴七喜".into(),class_id:3,age:24,grade:1};let students= vec![student_a, student_b, student_c, student_d];let class = Class{students,class_id:3,grade:1};let student_size = std::mem::size_of::<Student>(); // student结构体大小let student_align= std::mem::align_of::<Student>();//student内部对齐println!("student_size:{:?} student_align:{:?}",student_size,student_align);let class_ptr = &class as *const Class;let student_a_ptr = &class.students[0] as *const Student;println!("student_a value :{:?}",unsafe{&*student_a_ptr as &Student});//let student_b_ptr = unsafe{ student_a_ptr.add(1)}; // 也可以用add这种方式// 注意:offset不是偏移字节数,而是偏移元素个数!let student_b_ptr = unsafe{ student_a_ptr.offset(1)}; // offset方式let offset_between_a_and_b = unsafe{student_a_ptr.offset_from(student_b_ptr)};println!("offset_between_a_and_b:{}",offset_between_a_and_b);// -1println!("student_b value name :{:?}",unsafe{&*student_b_ptr as &Student}.name);//let student_c_ptr = unsafe{ student_a_ptr.add(2)};let student_c_ptr = unsafe{ student_a_ptr.offset(2)};let student_d_ptr = unsafe{ student_a_ptr.offset(3)};println!("student_b ref ptr address : {:p} convert raw ptr address : {:?}",&class.students[1],student_b_ptr);println!("student_b ref ptr address : {:p} convert raw ptr address : {:?}",&class.students[2],student_c_ptr);println!("student_b ref ptr address : {:p} convert raw ptr address : {:?}",&class.students[3],student_d_ptr);}
// 某某中学
// 班级
struct Class{students: Vec<Student>,//学生class_id: u32, //3班grade:u32, //高3
}
#[derive(Debug)]
struct Student{id: u32, //学号 2024000001;name: String, // 姓名,name="张三"class_id: u32, //班级,class_id=3,3班age: u32, //年龄grade: i8, //年级,grade=1,高中一年级
}
输出:
student_size:40 student_align:8
student_a value :Student { id: 20240001, name: "张三娃", class_id: 3, age: 14, grade: 1 }
offset_between_a_and_b:-1
student_a value :Student { id: 20240001, name: "张三娃", class_id: 3, age: 14, grade: 1 }
offset_between_a_and_b:-1
offset_between_a_and_b:-1
student_b value name :"李四牛"
student_b ref ptr address : 0x1c2ff7f6f48 convert raw ptr address : 0x1c2ff7f6f48
student_b ref ptr address : 0x1c2ff7f6f70 convert raw ptr address : 0x1c2ff7f6f70
student_b ref ptr address : 0x1c2ff7f6f98 convert raw ptr address : 0x1c2ff7f6f98
其实,你可以通过offset函数源码,可以了解更清楚,在offset内部,会再以bytes的倍数进行check_add。