How to overcome Stack Size issue with Visual Studio (running C codes with big array)

那年仲夏 提交于 2019-11-26 20:48:25

It seems that the reason behind this is the stack overflow. The issue can be resolved by increasing the stack size.
In visual studio you can do this by using /STACK:reserve[,commit]. Read the MSDN article.


For more detailed explanation:

Under Windows platforms, the stack size information is contained in the executable files. It can be set during compilation in Visual studio C++.
Alternatively, Microsoft provides a program editbin.exe which can change the executable files directly. Here are more details:

Windows (during compilation):

  1. Select Project->Setting.
  2. Select Link page.
  3. Select Category to Output.
  4. Type your preferred stack size in Reserve: field under Stack allocations. eg, 32768 in decimal or 0x20000 in hexadecimal.

Windows (to modify the executable file):

There are two programs included in Microsoft Visual Studio, dumpbin.exe and editbin.exe. Run dumpbin /headers executable_file, and you can see the size of stack reserve information in optional header values. Run editbin /STACK:size to change the default stack size.

Visual Studio does not work?

Although I don't regard VS as a valid development tool, I highly doubt that it would cause your problem.

128 * 128 is 16384. If you have too little stack space (on Windows, it's 1MB by default if I'm not mistaken), and you define an array of e. g. big enough structs (sized 64 bytes, more precisely), then this can easily cause a stack overflow, since automatic arrays are typically (although not necessarily) allocated on the stack.

It sounds like you're trying to declare large arrays on the stack. Stack memory is typically limited; it sounds like you're overflowing it.

You can fix this by giving your array static duration

static BigStruct arr[128][128];

or by dynamically allocating memory for it

BigStruct (*arr)[128] = malloc(sizeof(*arr) * 128);
// use arr
free(arr);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!