Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

rust - Thread '<main>' has overflowed its stack when allocating a large array using Box

I'm implementing combsort. I'd like to create fixed-size array on the stack, but it shows stack overflow. When I change it to be on the heap (Rust by Example says to allocate in the heap we must use Box), it still shows stack overflow.

fn new_gap(gap: usize) -> usize {
    let ngap = ((gap as f64) / 1.3) as usize;
    if ngap == 9 || ngap == 10 {
        return 11;
    }
    if ngap < 1 {
        return 1;
    }
    return ngap;
}

fn comb_sort(a: &mut Box<[f64]>) {
    // previously: [f64]
    let xlen = a.len();
    let mut gap = xlen;
    let mut swapped: bool;
    let mut temp: f64;
    loop {
        swapped = false;
        gap = new_gap(gap);
        for i in 0..(xlen - gap) {
            if a[i] > a[i + gap] {
                swapped = true;
                temp = a[i];
                a[i] = a[i + gap];
                a[i + gap] = temp;
            }
        }
        if !(gap > 1 || swapped) {
            break;
        }
    }
}

const N: usize = 10000000;

fn main() {
    let mut arr: Box<[f64]> = Box::new([0.0; N]); // previously: [f64; N] = [0.0; N];
    for z in 0..(N) {
        arr[z] = (N - z) as f64;
    }
    comb_sort(&mut arr);
    for z in 1..(N) {
        if arr[z] < arr[z - 1] {
            print!("!")
        }
    }
}

The output:

thread '<main>' has overflowed its stack
Illegal instruction (core dumped)

Or

thread 'main' has overflowed its stack
fatal runtime error: stack overflow

I know that my stack size is not enough, the same as C++ when creating a non-heap array that is too big inside a function, but this code is using heap but still shows stack overflow. What's really wrong with this code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the future, the box syntax will be stabilized. When it is, it will support this large allocation, as no function call to Box::new will be needed, thus the array will never be placed on the stack. For example:

#![feature(box_syntax)]

fn main() {
    let v = box [0i32; 5_000_000];
    println!("{}", v[1_000_000])
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...