我有一个需要 *const std::os::raw::c_char
的 C 函数我在 Rust 中做了以下事情:
use std::os::raw::c_char;
use std::ffi::{CString, CStr};
extern crate libc;
fn main() {
let _test_str: *const c_char = CString::new("Hello World").unwrap().as_ptr();
let fmt: *const c_char = CString::new("%s\n").unwrap().as_ptr();
unsafe { libc::printf(fmt, _test_str); }
unsafe {
let slice = CStr::from_ptr(_test_str);
println!("string buffer size without nul terminator: {}", slice.to_bytes().len());
}
}
但是,我无法获得
_test_str
打印出来,上面程序的输出很简单string buffer size without nul terminator: 0
如果我通过
_test_str
进入一些 C 函数,看到它是一个空字符串。我做错什么了?
最佳答案
您正在创建 CString
在与创建指向它的指针相同的语句中。 CString
拥有但未绑定(bind)到变量,因此它的生命周期与封闭语句一样长,导致指针无效。 documentation for as_ptr
专门警告了这一点。 :
For example, the following code will cause undefined behavior when ptr is used inside the unsafe block:
use std::ffi::{CString}; let ptr = CString::new("Hello").expect("CString::new failed").as_ptr(); unsafe { // `ptr` is dangling *ptr; }
This happens because the pointer returned by
as_ptr
does not carry any lifetime information and theCString
is deallocated immediately after theCString::new("Hello").expect("CString::new failed").as_ptr()
expression is evaluated.
您可以通过引入适用于整个函数的变量来解决问题,然后创建指向这些变量的指针:
fn main() {
let owned_test = CString::new("Hello World").unwrap();
let _test_str: *const c_char = owned_test.as_ptr();
let owned_fmt = CString::new("%s\n").unwrap();
let fmt: *const c_char = owned_fmt.as_ptr();
unsafe {
libc::printf(fmt, _test_str);
}
unsafe {
let slice = CStr::from_ptr(_test_str);
println!(
"string buffer size without nul terminator: {}",
slice.to_bytes().len()
);
}
// owned_fmt is dropped here, making fmt invalid
// owned_test is dropped here, making _test_str invalid
}
如果您使用原始指针,则需要格外小心,它们始终指向实时数据。引入变量是准确控制数据存在多长时间的最佳方法 - 从变量的初始化到变量超出范围的那一刻,它将存在。
关于reference - CString::new().unwrap().as_ptr() 给出空 *const c_char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202456/