How to allocate structs on the heap without taking up space on the stack in stable Rust?

☆樱花仙子☆ 提交于 2020-01-15 03:17:05

问题


In this code...

struct Test { a: i32, b: i64 }

fn foo() -> Box<Test> {              // Stack frame:
    let v = Test { a: 123, b: 456 }; // 12 bytes
    Box::new(v)                      // `usize` bytes (`*const T` in `Box`)
}

... as far as I understand (ignoring possible optimizations), v gets allocated on the stack and then copied to the heap, before being returned in a Box.

And this code...

fn foo() -> Box<Test> {
    Box::new(Test { a: 123, b: 456 })
}

...shouldn't be any different, presumably, since there should be a temporary variable for struct allocation (assuming compiler doesn't have any special semantics for the instantiation expression inside Box::new()).

I've found https://stackoverflow.com/a/31506225/5877243. Regarding my specific question, it only proposes the experimental box syntax, but mostly talks about compiler optimizations (copy elision).

So my question remains: using stable Rust, how does one allocate structs directly on the heap, without relying on compiler optimizations?


回答1:


You seem to be looking for the box_syntax feature, however as of Rust 1.39.0 it is not stable and only available with a nightly compiler. It also seems like this feature will not be stabilized any time soon, and might have a different design if it ever gets stabilized.

On a nightly compiler, you can write:

#![feature(box_syntax)]

struct Test { a: i32, b: i64 }

fn foo() -> Box<Test> {
    box Test { a: 123, b: 456 }
}



回答2:


Is there a way to allocate directly to the heap without box?

No. If there was, it wouldn't need a language change.

People tend to avoid this by using the unstable syntax indirectly, such as by using one of the standard containers which, in turn, uses it internally.

See also:

  • How to allocate arrays on the heap in Rust 1.0?
  • Is there any way to allocate a standard Rust array directly on the heap, skipping the stack entirely?
  • What does the box keyword do?
  • What is the <- symbol in Rust?



回答3:


As of Rust 1.39, there seems to be only one way in stable to allocate memory on the heap directly - by using std::alloc::alloc (note that the docs state that it is expected to be deprecated). It's reasonably unsafe.

Example:

#[derive(Debug)]
struct Test {
    a: i64,
    b: &'static str,
}

fn main() {
    use std::alloc::{alloc, dealloc, Layout};

    unsafe {
        let layout = Layout::new::<Test>();
        let ptr = alloc(layout) as *mut Test;

        (*ptr).a = 42;
        (*ptr).b = "testing";

        let bx = Box::from_raw(ptr);

        println!("{:?}", bx);
    }
}

This approach is used in the unstable method Box::new_uninit.

It turns out there's even a crate for avoiding memcpy calls (among other things): copyless. This crate also uses an approach based on this.



来源:https://stackoverflow.com/questions/59232877/how-to-allocate-structs-on-the-heap-without-taking-up-space-on-the-stack-in-stab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!