1

I have some analyses to do in Rust. As such, I have a rather large lookup table that creates a multidimensional array requiring 430,467,210 bytes. I've tried many times to load this into Rust, but I keep getting stack overflows. Recently I learned about heap allocation using Box and have tried to read my file into the boxed slice. It still does not work:

let mut temp_buffer = Box::new([0u8; 430467210]);
move_file.read(&mut *temp_buffer);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36

1 Answers1

3

A boxed array can still temporarily stack-allocate the array before moving it to the heap (especially in non-optimized builds). To ensure heap allocation from onset, use a vector instead:

let mut temp_buffer = vec![0u8; 430467210];
move_file.read(&mut temp_buffer);
Vladimir
  • 190
  • 5
  • Whoa, as slow as that is, it works. Thanks! – Joe Thomas Dec 27 '16 at 23:01
  • @wateriswet It's not supposed to be any slower than the array version (except for the time it takes to reserve the space on the heap, which should be dwarfed by the actual reading). How are you measuring the time? Are you compiling with optimizations turned on? – Vladimir Dec 28 '16 at 23:11