Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

placing large array inside Box causes stack overflow #28008

Closed
XAMPPRocky opened this issue Aug 26, 2015 · 2 comments
Closed

placing large array inside Box causes stack overflow #28008

XAMPPRocky opened this issue Aug 26, 2015 · 2 comments

Comments

@XAMPPRocky
Copy link
Member

I was trying to store a large array on the heap, the code produced a run-time error of stack overflow.

fn main() {
    let x = Box::new([0; 10_000_000]);
}

I expected the array to be initialised on the heap.

Instead it seems, the array is being initialised onto the stack, and then placed onto the heap, causing the this error message. thread '<main>' has overflowed its stack.

Meta

rustc --version --verbose:

rustc 1.2.0 (082e47636 2015-08-03)
binary: rustc
commit-hash: 082e4763615bdbe7b4dd3dfd6fc2210b7773edf5
commit-date: 2015-08-03
host: x86_64-apple-darwin
release: 1.2.0
@nagisa
Copy link
Member

nagisa commented Aug 26, 2015

This is a known issue with Box::new (as well as Rc::new et al). It will be resolved once box syntax (and placement-in for the general case) get stabilised. For now do

#[feature(box_syntax)]
fn main() {
    let x = box [0; 10_000_000];
}

on nightlies instead.

@steveklabnik
Copy link
Member

@nagisa is correct. While sometimes LLVM will do the right thing here, it will only be guaranteed with box.

Thank you for filing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants