获得一个堆栈溢出异常声明一个大阵时,

获得一个堆栈溢出异常声明一个大阵时,

问题描述:

以下code产生了对我一个堆栈溢出错误

The following code is generating a stack overflow error for me

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

我如何解决这个问题?我使用的Turbo C ++,但想保持我的code用C

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

编辑:

谢谢你的建议。在code以上只是举例来说,我居然在一个函数声明数组,而不是子主。另外,我需要的数组初始化为零,所以当我用Google搜索的malloc,我发现释放calloc很适合我的目的。

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也有优势分配让我用一个变量来声明大小的堆栈。

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

您数组是太大了,以适应进栈,可以考虑使用堆:

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.