声明大数组时出现堆栈溢出异常

发布于 2024-07-13 22:06:05 字数 359 浏览 10 评论 0原文

以下代码为我生成堆栈溢出错误,

int main(int argc, char* argv[])
{
    int sieve[2000000];
    return 0;
}

如何解决此问题? 我正在使用 Turbo C++,但想将我的代码保留在 C

编辑:

感谢您的建议。 上面的代码只是举例,我实际上是在函数中声明数组,而不是在 sub main 中。 另外,我需要将数组初始化为零,因此当我在 google 上搜索 malloc 时,我发现 calloc 非常适合我的目的。

与在堆栈上分配相比,Malloc/calloc 还具有允许我使用变量声明大小的优点。

The following code is generating a stack overflow error for me

int main(int argc, char* argv[])
{
    int sieve[2000000];
    return 0;
}

How do I get around this? I am using Turbo C++ but would like to keep my code in C

EDIT:

Thanks for the advice. The code above was only for example, I actually declare the array in a function and not in sub main. Also, I needed the array to be initialized to zeros, so when I googled malloc, I discovered that calloc was perfect for my purposes.

Malloc/calloc also has the advantage over allocating on the stack of allowing me to declare the size using a variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梨涡少年 2024-07-20 22:06:05

您的数组太大而无法放入堆栈,请考虑使用堆:

int *sieve = malloc(2000000 * sizeof(*sieve));

如果您确实想更改堆栈大小,看看这个文档。

提示: - 当动态分配的内存不再可用时,不要忘记释放它需要。

Your array is way too big to fit into the stack, consider using the heap:

int *sieve = malloc(2000000 * sizeof(*sieve));

If you really want to change the stack size, take a look at this document.

Tip: - Don't forget to free your dynamically allocated memory when it's no-longer needed.

故事和酒 2024-07-20 22:06:05

有 3 种方法:

  1. 在堆上分配数组 - 使用 malloc(),如其他海报建议的那样。 不要忘记free()它(尽管对于main()来说它并不那么重要——操作系统会在程序终止时为您清理内存)。
  2. 在单元级别声明数组 - 它将被分配在数据段中并且对每个人都可见(在声明中添加static将限制单元的可见性)。
  3. 将数组声明为static - 在这种情况下,它将分配在数据段中,但仅在main() 中可见。

There are 3 ways:

  1. Allocate array on heap - use malloc(), as other posters suggested. Do not forget to free() it (although for main() it is not that important - OS will clean up memory for you on program termination).
  2. Declare the array on unit level - it will be allocated in data segment and visible for everybody (adding static to declaration will limit the visibility to unit).
  3. Declare your array as static - in this case it will be allocated in data segment, but visible only in main().
清晰传感 2024-07-20 22:06:05

大约有 7MB 的堆栈空间。 在 Visual Studio 中,您可以使用 /STACK:###,### 来反映您想要的大小。 如果你确实想要一个巨大的堆栈(可能是一个很好的理由,使用 LISP 或其他东西:),甚至在强制你使用 VirtualAlloc 之前堆仅限于小型分配),你可能还需要将你的 PE 设置为使用/LARGEADDRESSAAWARE(又是 Visual Studio 的链接器),但此配置是您的 PE 标头,以允许编译的二进制文件寻址完整的 4GB 32 位地址空间(如果在 WOW64 中运行)。 如果构建真正庞大的二进制文件,您通常还需要将 /bigobj 配置为附加链接器参数。

如果您仍然需要更多空间,您可以通过使用类似于(再次是 MSVC 的链接)/merge: 的内容来从根本上违反约定,这将允许您将一个部分打包到另一个部分中,这样您就可以将每个字节用于单个共享代码/数据部分。 当然,您还需要在 def 文件中或使用 #pgrama 配置 SECTIONS 权限。

That's about 7MB of stack space. In visual studio you would use /STACK:###,### to reflect the size you want. If you truely want a huge stack (could be a good reason, using LISP or something :), even the heap is limited to small'sh allocations before forcing you to use VirtualAlloc), you may also want to set your PE to build with /LARGEADDRESSAAWARE (Visual Studio's linker again), but this configure's your PE header to allow your compiled binary to address the full 4GB of 32'bit address space (if running in a WOW64). If building truely massive binaries, you would also typically need to configure /bigobj as an additional linker paramerter.

And if you still need more space, you can radically violate convention by using something simular to (again MSVC's link) /merge:, which will allow you to pack one section into another, so you can use every single byte for a single shared code/data section. Naturally you would also need to configure the SECTIONS permissions in a def file or with #pgrama.

对岸观火 2024-07-20 22:06:05

使用malloc。 所有检查返回类型是否为空,如果为空,则您的系统根本没有足够的内存来容纳那么多值。

Use malloc. All check the return type is not null, if it is null then your system simply doesn't have enought memory to fit that many values.

待"谢繁草 2024-07-20 22:06:05

您最好将其分配在堆上,而不是堆栈上。 就像是

int main(int argc, char* argv[])
{
    int * sieve;
    sieve = malloc(20000);
    return 0;
}

You would be better off allocating it on the heap, not the stack. something like

int main(int argc, char* argv[])
{
    int * sieve;
    sieve = malloc(20000);
    return 0;
}
地狱即天堂 2024-07-20 22:06:05

你的数组很大。

您的计算机或操作系统可能没有或不想分配这么多内存。


如果您绝对需要一个巨大的数组,您可以尝试动态分配它(使用malloc(...)),但是这样您就会面临内存泄漏的风险。 不要忘记释放内存。

malloc 的优点是它尝试在堆而不是堆栈上分配内存(因此不会出现堆栈溢出)。

您可以检查 malloc 返回的值来查看分配是否成功或失败。
如果失败,只需尝试分配一个较小的数组。


另一种选择是使用可以动态调整大小的不同数据结构(如链接列表)。 这个选项是否好取决于您要如何处理数据。

另一种选择是将内容存储在文件中,动态传输数据。 这种方法是最慢的。

如果您选择硬盘上的存储,您不妨使用现有的库(用于数据库)

Your array is huge.

It's possible that your machine or OS don't have or want to allocate so much memory.


If you absolutely need an enormous array, you can try to allocate it dynamically (using malloc(...)), but then you're at risk of leaking memory. Don't forget to free the memory.

The advantage of malloc is that it tries to allocate memory on the heap instead of the stack (therefore you won't get a stack overflow).

You can check the value that malloc returns to see if the allocation succeeded or failed.
If it fails, just try to malloc a smaller array.


Another option would be to use a different data structure that can be resized on the fly (like a linked list). Wether this option is good depends on what you are going to do with the data.

Yet another option would be to store things in a file, streaming data on the fly. This approach is the slowest.

If you go for storage on the hard drive, you might as well use an existing library (for databases)

や莫失莫忘 2024-07-20 22:06:05

由于 Turbo C/C++ 是 16 位编译器,int 数据类型大约消耗 2 个字节。
2字节*2000000=40,00,000字节=3.8147MB空间。

函数的自动变量存储在堆栈中,导致堆栈内存溢出。 相反,使用数据内存[使用静态或全局变量]或动态堆内存[使用malloc/calloc]根据处理器内存映射的可用性创建所需的内存。

As Turbo C/C++ is 16 bit compiler int datatype consumes about 2 bytes.
2bytes*2000000=40,00,000 bytes=3.8147MB space.

The auto variables of a function is stored in stack and it caused the overflow of the stack memory. Instead use the data memory [using static or global variable] or the dynamic heap memory [using the malloc/calloc] for creating the required memory as per the availability of the processor memory mapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文