4

I have a large object

pub(crate) struct Grid {
    pub weights: [[[f32; GRID_SIZE]; GRID_SIZE]; GRID_SIZE],
    // ... other attributes
}

With a large grid size, it does not fit on the stack and causes a stack overflow. I tried to create a Box, but Box::new(Grid { .. }) still overflows since Rust tries to create the entire object on the stack and copy it to the heap afterwards. How do I create some type of box or reference immediately for objects that don't fit on the stack?

I found How to allocate arrays on the heap in Rust 1.0?, but this works only for a array and not for an arbitrary struct. I assume there must be a good way to create a pointer directly without converting back and forth to a vector.

The box Grid {} syntax seems to not work either, Rust complains that that syntax is experimental.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
mousetail
  • 7,009
  • 4
  • 25
  • 45

0 Answers0