How can I get an array or a slice from a raw pointer?

13,742

If you want to obtain a slice from a raw pointer, use std::slice::from_raw_parts():

let slice = unsafe { std::slice::from_raw_parts(some_pointer, count_of_items) };

If you want to obtain a mutable slice from a raw pointer, use std::slice::from_raw_parts_mut():

let slice = unsafe { std::slice::from_raw_parts_mut(some_pointer, count_of_items) };

Are you sure you want read()? Without special care it will cause disaster on structs with destructors. Also, read() does not read a value of some specified type from a pointer to bytes; it reads exactly one value of the type behind the pointer (e.g. if it is *const u8 then read() will read one byte) and returns it.

If you only want to write byte contents of a structure into a vector, you can obtain a slice from the raw pointer:

use std::mem;
use std::io::Write;

struct SomeStruct {
    a: i32,
}

fn main() {
    let some_struct = SomeStruct { a: 32 };

    let mut v: Vec<u8> = Vec::new();
    let view = &some_struct as *const _ as *const u8;
    let slice = unsafe { std::slice::from_raw_parts(view, mem::size_of::<SomeStruct>()) };
    v.write(slice).expect("Unable to write");

    println!("{:?}", v);
}

This makes your code platform-dependent and even compiler-dependent: if you use types of variable size (e.g. isize/usize) in your struct or if you don't use #[repr(C)], the data you wrote into the vector is likely to be read as garbage on another machine (and even #[repr(C)] may not lift this problem sometimes, as far as I remember).

Share:
13,742
viraptor
Author by

viraptor

Dev-ops monkey interested in clouds Using: C / python / many exotic languages

Updated on June 05, 2022

Comments

  • viraptor
    viraptor about 2 years

    Can I somehow get an array from std::ptr::read?

    I'd like to do something close to:

    let mut v: Vec<u8> = ...
    let view = &some_struct as *const _ as *const u8;
    v.write(&std::ptr::read<[u8, ..30]>(view));
    

    Which is not valid in this form (can't use the array signature).

  • viraptor
    viraptor over 9 years
    It's for reading from a network packet, so yeah, that's exactly what I needed.
  • Bruno Grieder
    Bruno Grieder over 4 years
    Any idea if after creating the slice, the memory is freed when the slice is dropped ? The doc is unclear and I am getting "double free" errors when calling the C function supposed to release the memory previously allocated (on the C side).