以下是铁 rust 代码:
use std::alloc;
fn main() {
let name = String::from("a");
let obj = unsafe { GcObject::allocate(name) };
println!("{:?}", obj.object);
}
pub struct GcObject {
object: *mut Object,
}
impl GcObject {
pub unsafe fn allocate(object: String) -> Self {
let layout = alloc::Layout::new::<Object>();
// Allocate the object
let ptr = alloc::alloc(layout) as *mut Object;
if ptr.is_null() {
eprintln!("Alloc error!");
alloc::handle_alloc_error(layout);
}
*ptr = Object { kind: object };
GcObject { object: ptr }
}
}
struct Object {
kind: String,
}
Rustc version: rustc 1.70.0 (90c541806 2023-05-31)
Platform: Fedora Linux 38
When compiled with following runs fine:
rustc main.rs -C opt_level=0
But when compiled with optimizations enabled like:
rustc main.rs -C opt_level=3
Causes a crash when run with the following message:
Illegal instruction(core dumped)
个
那么,有没有办法解决这个问题,或者我的代码有问题.