Stack Overflow Exception when declaring multidimensional arrays

后端 未结 3 1335
你的背包
你的背包 2021-01-21 18:59

I\'m somewhat new to programming in general and I\'ve run into an issue with declaring 3D and 4D arrays. I have several declarations like this at the start of my main function,

3条回答
  •  一个人的身影
    2021-01-21 19:27

    Assuming for simplicity that sizeof(string) == 2 (it's probably more), you're trying to allocate (64^3)*9*2 bytes on the stack. That comes out to 4,718,592 bytes, or approximately 4.5 MiB. Most likely, you just don't have 4.5 MiB available on your stack.

    Since these variables are declared in main(), you have two possible solutions:

    1. Declare them static.

    2. Declare them outside main(), as global variables.

    This will cause them to be allocated before the program starts, rather than on the stack. The only difference between the two approaches is whether they'll be visible in other functions.

    There may also be a way to tell your compiler that the program needs more stack space, but I think making them static is probably the better solution here. If they were in a function other than main() though, you'd probably need to do something else.

提交回复
热议问题