I want to expose a "dynamic array" to a C function. The C function will own the data and later will call a function of mine to free the data. So it'll look something like the following:
fn get_something(len: *mut usize) -> *mut u8;
fn dealloc_something(data: *mut u8, len: usize);
Internally I have a Box<[T]>
(my_vec.to_boxed_slice()
). I can get the size/length pretty easily, but I don't know which pointer I should return. If I pass the pointer returned from boxed_slice.as_mut_ptr()
to Box::from_raw()
, the application crashes. However, if I pass the pointer returned from Box::into_raw
, I can't find a guarantee of memory layout (the pointer points to the first element of the array and will continue to do so for all future Rust versions).
What's the solution here?