# Main Overflows its Stack at Array Initialization

**URL:** https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794
**Category:** help
**Created:** [November 19, 2019, 8:17pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794 "2019-11-19T20:17:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tfinnegan937](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tfinnegan937](https://users.rust-lang.org/u/tfinnegan937)
#### Post date: [November 19, 2019, 8:17pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/1 "2019-11-19T20:17:01Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)
#### Post date: [November 19, 2019, 8:25pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/2 "2019-11-19T20:25:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![HadrienG](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/hadrieng/32/3798_2.png) [@HadrienG](https://users.rust-lang.org/u/HadrienG)
#### Post date: [November 19, 2019, 8:46pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/3 "2019-11-19T20:46:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tfinnegan937](https://avatars.discourse-cdn.com/v4/letter/t/97f17d/32.png) [@tfinnegan937](https://users.rust-lang.org/u/tfinnegan937)
#### Post date: [November 19, 2019, 8:54pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/4 "2019-11-19T20:54:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)
#### Post date: [November 19, 2019, 10:45pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/5 "2019-11-19T22:45:24Z")

</div>

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

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

```

- [Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f08e35fc7fca916b6317ffecadd0e8b9)

* * *

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.

---

<div class="post-metadata">

### Author: ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)
#### Post date: [February 17, 2020, 10:53pm UTC](https://users.rust-lang.org/t/main-overflows-its-stack-at-array-initialization/34794/6 "2020-02-17T22:53:37Z")

</div>

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