简体   繁体   中英

Rust thread 'main' has overflowed its stack

I'm trying to re-implement this C code:

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#define ROWS 100000
#define COLS 10000

int twodarray[ROWS][COLS];

int main() {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            twodarray[i][j] = rand();
        }
    }

    int64 sum = 0;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            sum += twodarray[i][j];
        }
    }
}

So, after a lot of trial and error I've come up with Rust code that at least compiles

extern crate rand;

const ROWS: usize = 100000;
const COLS: usize = 10000;

fn main() {
    let mut twodarray: [[u128; COLS]; ROWS] = [[0; COLS]; ROWS];

    for i in 0..ROWS {
        for j in 0..COLS {
            twodarray[i][j] = rand::random::<u8>() as u128;
        }
    }

    let mut sum: u128 = 0;
    for k in 0..ROWS {
        for j in 0..COLS {
            sum += twodarray[k][j] as u128;
        }
    }
    println!("{}", sum);
}

But when I compile and execute it, I get the following error message: thread 'main' has overflowed its stack . To be honest, I have absolutelly no idea why this is happening. I'm currently learning rust, but there is not a lot of information on 2d arrays online... I DO NOT WANT TO USE VECTOR. This exercise is specifically aimed on arrays.

EDIT: After reading the accepted answear, I've come up with Rust code that outputs expected results:

extern crate rand;

const ROWS: usize = 100000;
const COLS: usize = 10000;

fn main() {
    // let mut twodarray: Box<[[u8; COLS]; ROWS]> = Box::new([[0; COLS]; ROWS]);
    // for i in 0..ROWS {
    //     for j in 0..COLS {
    //         twodarray[i][j] = rand::random::<u8>();
    //     }
    // }

    // let mut sum: u32 = 0;
    // for k in 0..ROWS {
    //     for l in 0..COLS {
    //         sum += twodarray[k][l] as u32;
    //     }
    // }

    let mut twodarray: Box<[[u8; ROWS]; COLS]> = Box::new([[0; ROWS]; COLS]);
    for i in 0..COLS {
        for j in 0..ROWS {
            twodarray[i][j] = rand::random::<u8>();
        }
    }

    let mut sum: u32 = 0;
    for k in 0..COLS {
        for l in 0..ROWS {
            sum += twodarray[k][l] as u32;
        }
    }
    println!("{}", sum);
}

This two-dimensional array is huge. Rust tries to allocate it on the stack (that's the way arrays in Rust work).

You explicitly wrote you didn't want to use vectors, but in reality vectors are allocated on the heap, so you wouldn't have such a problem with them. It's good to keep that in mind.

If you insist on using arrays, maybe put them in a Box , so that they go to the heap?

That's not a Rust-specific problem. Stacks are generally a lot more limited, compared to the heap, usually up to a few megabytes. In some other languages, such as Java, arrays are allocated on the heap, but that doesn't mean there is no similar stack size limit there as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM