3
votes

I'm trying to create and return a C++ struct. I am currently getting a cannot move out of dereference of raw pointer error when I try to compile. Any idea how I can make this work?

#![allow(non_snake_case)]
#![allow(unused_variables)]

extern crate octh;

// https://thefullsnack.com/en/string-ffi-rust.html
use std::ffi::CString;

#[no_mangle]
pub unsafe extern "C" fn Ghelloworld(
    shl: *const octh::root::octave::dynamic_library,
    relative: bool,
) -> *mut octh::root::octave_dld_function {
    let name = CString::new("helloworld").unwrap();
    let pname = name.as_ptr() as *const octh::root::std::string;
    std::mem::forget(pname);

    let doc = CString::new("Hello World Help String").unwrap();
    let pdoc = doc.as_ptr() as *const octh::root::std::string;
    std::mem::forget(pdoc);

    return octh::root::octave_dld_function_create(Some(Fhelloworld), shl, pname, pdoc);
}

pub unsafe extern "C" fn Fhelloworld(
    args: *const octh::root::octave_value_list,
    nargout: ::std::os::raw::c_int,
) -> octh::root::octave_value_list {
    let list: *mut octh::root::octave_value_list = ::std::ptr::null_mut();
    octh::root::octave_value_list_new(list);
    std::mem::forget(list);
    return *list;
}
1

1 Answers

7
votes

I'm trying to create and return a C++ struct

You cannot; C++ (like Rust) does not have a stable, defined ABI. There is no way in Rust to specify that a structure has repr(C++), therefore you cannot create such a structure, much less return it.

The only stable ABI is the one presented by C. You can define structs as repr(C) to be able to return them directly:

extern crate libc;

use std::ptr;

#[repr(C)]
pub struct ValueList {
    id: libc::int32_t,
}

#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
    let list_ptr = ::std::ptr::null_mut();
    // untested, will cause segfault unless list_ptr is set
    unsafe { ptr::read(list_ptr) }
}

That method is highly suspicious though; normally you'd see it as

#[no_mangle]
pub extern "C" fn hello_world() -> ValueList {
    unsafe {
        let mut list = mem::uninitialized();
        list_initialize(&mut list);
        list
    }
}

See also:


I encourage you to read my Rust FFI Omnibus.