Main Overflows its Stack at Array Initialization

Heya,

I'm writing an emulator for an old gaming system.

I want an array of bytes, with a number of entries equaling 16 MB, with overflowing variables.

I use the following line to do so:

let file_init : [Wrapping<u8>; 0x1000000];

I receive the error "thread 'main' has overflowed its stack".

Am I somehow misunderstanding the initialization of arrays? How is this exceeding my allocated stack memory?

I'm not sure what the situation is now, but last I heard the stack size of the main thread is 8 MiB, so you should not be surprised to see 16 MiB of data overflow the stack. Use a Vec instead to put the data on the heap.

3 Likes

On Linux, you can query the stack size limit of applications using ulimit -s, and disable it for the current shell using ulimit -s unlimited. Not sure how that's done on other operating systems.

1 Like

I was aware of how the stack worked, but not how small it was. This sorted my issue.

You can also get big stacks in a portable way by using an additional thread:

    ::std::thread::Builder::new()
        .stack_size(50 * Mi)
        .spawn(move || { ... })
        .unwrap()
        .join()
        .unwrap()

In this case, however, I'd rather use global memory for your big array, either through static memory or through dynamically allocated memory, such as vec![]'s.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.