C语言 如何克服 Visual Studio 的堆栈大小问题(使用大数组运行 C 代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20744650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:32:20  来源:igfitidea点击:

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

cwindowsvisual-studiostack-overflow

提问by user3129690

I am using Visual Studio 13 to compile c codes for the first time. The codes run perfectly O.K. with 2d arays of size 64*64 (there are a few arrays in my programme) but if I increase the array size to 128*128 it does not run (but compile correctly). Instead it gives a message ".exe has stopped working". My machine has 4GB ram and the same programme run with 128*128 array if I run the codes from linux.

我第一次使用 Visual Studio 13 编译 c 代码。代码在大小为 64*64 的 2d 数组上运行完全正常(我的程序中有几个数组),但是如果我将数组大小增加到 128*128,它就不会运行(但可以正确编译)。相反,它给出了一条消息“.exe 已停止工作”。如果我从 linux 运行代码,我的机器有 4GB ram,并且相同的程序以 128*128 阵列运行。

Let me provide some more details: I have run the same code from linux using Intel C Compiler (non-commercial version) in the same machine. But due to some problem I am now constrained to work from a Windows environment. I searched and have installed two c- compilers (1) Visual Studio 13 and (2) Borland C. Both work well with a small array. But the moment I increase array size Visual Studio give the message ".exe has stopped working". I compile the programme using 'cl' from "Developers Command Prompt VS 13".

让我提供更多细节:我在同一台机器上使用英特尔 C 编译器(非商业版)从 linux 运行相同的代码。但是由于一些问题,我现在只能在 Windows 环境中工作。我搜索并安装了两个 c 编译器 (1) Visual Studio 13 和 (2) Borland C。两者都适用于小数组。但是当我增加数组大小时,Visual Studio 会给出消息“.exe 已停止工作”。我使用“开发人员命令提示符 VS 13”中的“cl”编译程序。

I feel the problem is with stack size.

我觉得问题在于堆栈大小。

In the link detailed explanation(as provided below) I have seen a command "ulimit" used in linux environment to increase the stack size. I remember using it a few years ago.

在链接详细说明(如下提供)中,我看到了在 linux 环境中使用的命令“ulimit”来增加堆栈大小。我记得几年前用过它。

I feel we are close to the solution, but my problem with Windows (and VS 2013) persists as I failed to execute dumpbin /headers executable_fileor editbin /STACK:size. Actually I feel I do not know how to execute them. I tried to execute them from "Developer Command Prompt VS 13" as well as using Run (windows start bottom->search (run)->Run (prop up)). I request you kindly to provide more details if possible.

我觉得我们已经接近解决方案了,但是我的 Windows(和 VS 2013)问题仍然存在,因为我无法执行dumpbin /headers executable_fileeditbin /STACK:size. 其实我觉得我不知道如何执行它们。我尝试从“开发人员命令提示符 VS 13”以及使用运行(Windows 开始底部->搜索(运行)->运行(支撑))执行它们。如果可能,请提供更多详细信息。

I searched and found this website and think here the solution can be found.

我搜索并找到了这个网站,并认为可以在这里找到解决方案。

Please help. I want to run using Visual Studio 13 from Windows.

请帮忙。我想从 Windows 使用 Visual Studio 13 运行。

回答by haccks

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.

看来这背后的原因是堆栈溢出。该问题可以通过增加堆栈大小来解决。
在 Visual Studio 中,您可以使用/STACK:reserve[,commit]. 阅读MSDN 文章



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.exewhich can change the executable files directly. Here are more details:

在 Windows 平台下,堆栈大小信息包含在可执行文件中。它可以在 Visual Studio C++ 编译期间设置。
或者,微软提供了一个editbin.exe可以直接更改可执行文件的程序。以下是更多详细信息:

Windows (during compilation):

Windows(编译期间):

  1. Select Project->Setting.
  2. Select Linkpage.
  3. Select Category to Output.
  4. Type your preferred stack size in Reserve:field under Stack allocations. eg, 32768in decimal or 0x20000in hexadecimal.
  1. 选择Project->Setting
  2. 选择Link页面。
  3. 选择Category to Output
  4. Reserve:下的字段中键入您的首选堆栈大小Stack allocations。例如,32768十进制或0x20000十六进制。

Windows (to modify the executable file):

Windows(修改可执行文件):

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

Microsoft Visual Studio 中包含两个程序,dumpbin.exe以及editbin.exe. 运行dumpbin /headers executable_file,就可以看到中的size of stack reserve信息了optional header values。运行 editbin /STACK:size以更改默认堆栈大小。

回答by user3129690

Visual Studio does not work?

Visual Studio 不起作用?

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

虽然我不认为 VS 是一个有效的开发工具,但我非常怀疑它会导致您的问题。

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.

128 * 128 是 16384。如果您的堆栈空间太小(在 Windows 上,如果我没记错的话,默认情况下它是 1MB),并且您定义了一个数组,例如足够大的structs(大小为 64 字节,更准确地说),那么这个很容易导致堆栈溢出,因为通常(尽管不一定)在堆栈上分配自动数组。

回答by zeeshan ali

A complete Visual Studio layout requires a minimum of 35 GB of disk space. For more information, see System requirements. And for information about how to create a layout with only the components you want to install, see Use command-line parameters to install Visual Studio.

完整的 Visual Studio 布局至少需要 35 GB 的磁盘空间。有关更多信息,请参阅系统要求。有关如何创建仅包含要安装的组件的布局的信息,请参阅使用命令行参数安装 Visual Studio。

回答by simonc

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);