dimanche 4 juin 2023

Why am I running into stack overflows when calling the shuffle method on large usize arrays within structs in Rust?

I'm new to Rust and i've ran into a problem using the shuffle() method on a usize array like so:

self.rand_list.shuffle(&mut rand::thread_rng());

Using the method sometimes results in a stack overflow (but not always) when the array gets too big (i've started noticing the problem for sizes close to 20 000 elements).

Since i'm not yet very familiar with the language i'm having a hard time reading through the documentation to understand why this is happening and why the amount of items pushed on the stack varries between executions. Note that I cannot replicate this error when the array is not part of a struct.

The point of rand_list is to contain a list of indexes that my code is meant to modifiy, one at a time. the struct containing rand_list also possesses a field used as a counter that is incremented each time I read an index from rand_list.

I was initially generating random indexes one at a time with a thread_rng() and was met with the same problem. So i tried to switch to this solution instead.

The struct used here uses 3 other arrays of similar size, so even combined they should be about 10 times smaller than the default stack size on windows which i've come to understand is around 1Mb long. So i don't believe the issue lies here.




Aucun commentaire:

Enregistrer un commentaire