9
votes

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 Do values in return position always get allocated in the parents stack frame or receiving Box?. 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?

4
this will be a totally absurd thing to do let the compiler to the magic for you. But if you want doc.rust-lang.org/std/alloc/trait.Alloc.html.Stargateur
Thanks for the link. However, I strongly disagree about absurdity. I think any "magic" is inherently unreliable, let alone that it requires knowledge of compiler internals. Moreover, when performance is critical, or resources are limited, all memory operations are important, and in this particular case it would be nice to be sure that no unnecessary operations are performed.mrnateriver
I should also note that even though the provided link technically solves the problem, I'm pretty sure it's not the idiomatic way. It's just too low level. I could've called an allocator directly for that matter, probably, and get the raw pointer to the heap. I'm pretty sure there must be a language-level (or "stdlib-level") construct for that.mrnateriver
An other "solution" is to use doc.rust-lang.org/std/boxed/struct.Box.html#method.new_uninit that new so I didn't think to it before, still low level in my opinion. But use this to "avoid using stack" is stupid.Stargateur

4 Answers

9
votes

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 }
}
8
votes

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.

3
votes

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:

-1
votes

I recently had the same problem. Based on the answers here and other places, I wrote a simple function for heap allocation:

pub fn unsafe_allocate<T>() -> Box<T> {
    let mut grid_box: Box<T>;
    unsafe {
        use std::alloc::{alloc, dealloc, Layout};
        let layout = Layout::new::<T>();
        let ptr = alloc(layout) as *mut T;
        grid_box = Box::from_raw(ptr);
    }
    return grid_box;
}

This will create a region in memory automatically sized after T and unsafely convince Rust that that memory region is an actual T value. The memory may contain arbitrary data; you should not assume all values are 0.

Example use:

let mut triangles: Box<[Triangle; 100000]> = unsafe_allocate::<[Triangle; 100000]>();