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

Unexpected stack overflow when initializing array #205

Open
camilchp opened this issue Aug 11, 2022 · 2 comments
Open

Unexpected stack overflow when initializing array #205

camilchp opened this issue Aug 11, 2022 · 2 comments

Comments

@camilchp
Copy link

The following code generates a stack overflow, despite containing no explicitly recursive code.

extern crate lazy_static; // 1.4.0

use lazy_static::lazy_static;

const SIZE : usize = 1_000_000;
const U0 : usize = 0;

fn u_gen() -> [usize;SIZE] {

        let mut u = [U0;SIZE];
        for i in 1..SIZE {
            u[i] = (u[i-1] + 1) % 10;
        } 
        u
}
 
lazy_static! {
    static ref U : [usize;SIZE] = u_gen();
}


fn main() {
    dbg!((*U)[0]);
    //dbg!(u_gen()[0]);
}

playground link
On my laptop, the stack overflow occurs in Debug mode, but not in Release mode.

On the playground, on the other hand, I get a stack overflow in both modes.

The problem only occurs when u_gen is called in lazy_static. When calling it directly (see commented line in main), everything goes fine.

cc @pchampin

@JosuGZ
Copy link

JosuGZ commented Aug 30, 2022

That is a big array to be on the stack. I'm experiencing the same problem with a smaller one though (80Kb).

I'm not sure if lazy_static boxes the value but before that I think it creates it on the stack, and it makes copies of it.

@pchampin
Copy link

Agreed, such a big array on the stack may not be ideal. But the fact that it works without lazy_static and fails with it makes it a bug, regardless.

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