Search code examples
ruststack-overflow

Why does this Rust code cause a stack overflow without recursion or looping?


I cannot figure out why this Rust code causes a stack overflow.

enum SlidingAttackTable {
    Rook([[u64; 4096]; 64]),
    Bishop([[u64; 512]; 64]),
}

fn init_all() -> (SlidingAttackTable, SlidingAttackTable, [u64; 64], [u64; 64], [usize; 64]) {
    return (SlidingAttackTable::Bishop([[0; 512]; 64]), SlidingAttackTable::Rook([[0; 4096]; 64]), [0; 64], [0; 64], [0; 64]);
}

fn main() {
    println!("start");
    let (bishop_attack_table, rook_attack_table, bishop_masks, bishop_magic_numbers, bishop_relevant_bits) = init_all();
}

I have tried commenting out everything but println!("start") and it printed "start" with no error. Somehow, when I add just these few lines of code back in, it crashes before even getting to that line and printing "start". There is no looping or recursion going on here that I can detect, so I have no idea how to begin diagnosing this problem.


Solution

  • [u64; 4096] takes 2^(3+12) bytes. That's 2^15 bytes. And [[u64; 4096]; 64] takes 64 times of that (i.e. 2^21 bytes, or 2 MB). It seems you have hit the stack size limit.

    You could increase the stack size limit, but that would probably be a bad idea. Overall, you shouldn't try to keep too much data on the stack. Use the heap instead. There are multiple ways to do that. If you are using a vector, only a pointer will be held on the stack and the data itself will be on the heap.