Skip to content

Unexpected stack overflow when initializing array #205

@camilchp

Description

@camilchp

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions