Will `Box<T>` allocate memory by declaration?

#1

Below will cause the stack overflow:

let foo: [i32; 10000000]; 

Below will not cause the stack overflow, but I wonder if it will allocate memory on the heap without initialization (Box<T>::new):

let bar: Box<[i32; 10000000]>;

Update

Just realized after reading the source:

https://doc.rust-lang.org/src/alloc/boxed.rs.html#94

pub struct Box<T: ?Sized>(Unique<T>);
#2

It won’t. All that does is reserve space on the stack for the Box pointer, but it can’t put anything in it until you allocate something.

2 Likes
#3

Sadly it won’t. Issue on GH https://github.com/rust-lang/rust/issues/53827