简体   繁体   English

Rust 线程“主”已溢出其堆栈

[英]Rust thread 'main' has overflowed its stack

I'm trying to re-implement this C code:我正在尝试重新实现这个 C 代码:

#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所以,经过大量的试验和错误,我想出了至少可以编译的 Rust 代码

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 .但是当我编译并执行它时,我收到以下错误消息: 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.我目前正在学习 rust,但是网上关于 2d arrays 的信息并不多……我不想使用矢量。 This exercise is specifically aimed on arrays.本练习专门针对 arrays。

EDIT: After reading the accepted answear, I've come up with Rust code that outputs expected results:编辑:阅读接受的答案后,我想出了输出预期结果的 Rust 代码:

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). Rust 尝试在堆栈上分配它(这就是 Rust 中 arrays 的工作方式)。

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?如果你坚持使用 arrays,也许把它们放在一个Box里,让它们 go 到堆里?

That's not a Rust-specific problem.这不是 Rust 特有的问题。 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.在其他一些语言中,例如 Java、arrays 是在堆上分配的,但这并不意味着那里也没有类似的堆栈大小限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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