0

I need to create a huge NxN array. Simple arrays are created on the stack, so no success there. Static arrays have to be const or unsafe mut, so that's a no.

I tried Boxing that array:

const N: usize = 1000000;
let mut boxed: Box<[usize; N]> = Box::new([0; N]);
boxed[1] = 1;

But that overflows the stack anyway, presumably, because it creates a temporary array that is then copied into a Box.

I tried a Vec of arrays:

const N: usize = 1000000;
let mut v = Vec::<[usize; N]>::with_capacity(10);

v.push([0; N]);

with the same result. As far as I understand with_capacity only allocates memory; since Rust has no constructors, I still have to push (i.e.) copy something into that memory.

So, what is the proper way of doing that without going nightly for placement new?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Amomum
  • 6,217
  • 8
  • 34
  • 62

1 Answers1

0

You could do it in a dedicated thread with a greater stack size:

use std::thread;

fn main() {
    const N: usize = 10000000;

    let builder = thread::Builder::new().name("reductor".into()).stack_size(
        1024 * 1024 *
            1024,
    );

    let handler = builder
        .spawn(|| {
            let mut v = Vec::<[usize; N]>::with_capacity(10);
            v.push([0; N]);

            println!("{:?}", v[0][0]);
        })
        .unwrap();

    handler.join().unwrap();
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ljedrz
  • 20,316
  • 4
  • 69
  • 97