2 ms·
It might be good to mention how to free the memory. By passing the pointer back to a rust function like #[no_mangle] pub extern fn free_string(p: *mut c_ch
by thayne 9y ago
It might be good to mention how to free the memory. By passing the pointer back to a rust function like
#[no_mangle]
pub extern fn free_string(p: *mut c_char) {
unsafe {
let _ = CString::from_raw(p);
// optionally you could explicitly call drop, but
// that is unnecessary, since it is automatically dropped
// at end of scope
}
}
Also the `into_raw` method on CString gives you a pointer while simultaneously taking ownership of the CString, so mem::forget isn't necessary if you use `into_raw` rather than `as_ptr`.