C++ 大数组大小的分段错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1847789/
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
Segmentation fault on large array sizes
提问by Mayank
The following code gives me a segmentation fault when run on a 2Gb machine, but works on a 4GB machine.
以下代码在 2Gb 机器上运行时给我一个分段错误,但在 4GB 机器上运行。
int main()
{
int c[1000000];
cout << "done\n";
return 0;
}
The size of the array is just 4Mb. Is there a limit on the size of an array that can be used in c++?
阵列的大小仅为 4Mb。可以在 C++ 中使用的数组的大小是否有限制?
回答by Charles Salvia
You're probably just getting a stack overflow here. The array is too big to fit in your program's stack address space.
您可能只是在这里遇到堆栈溢出。数组太大,无法放入程序的堆栈地址空间。
If you allocate the array on the heap you should be fine, assuming your machine has enough memory.
如果你在堆上分配数组你应该没问题,假设你的机器有足够的内存。
int* array = new int[1000000];
int* array = new int[1000000];
But remember that this will require you to delete[]
the array. A better solution would be to use std::vector<int>
and resize it to 1000000 elements.
但请记住,这将需要您访问delete[]
数组。更好的解决方案是使用std::vector<int>
并将其调整为 1000000 个元素。
回答by Gunther Piez
In C or C++ local objects are usually allocated on the stack. You are allocating a large array on the stack, more than the stack can handle, so you are getting a stackoverflow.
在 C 或 C++ 中,本地对象通常在堆栈上分配。您在堆栈上分配了一个大数组,超出了堆栈的处理能力,因此您得到了一个stackoverflow。
Don't allocate it local on stack, use some other place instead. This can be achieved by either making the object globalor allocating it on the global heap. Global variables are fine, if you don't use the from any other compilation unit. To make sure this doesn't happen by accident, add a static storage specifier, otherwise just use the heap.
不要在堆栈上本地分配它,而是使用其他地方。这可以通过使对象成为全局对象或在全局堆上分配对象来实现。如果您不使用来自任何其他编译单元的 全局变量就可以了。为确保这不会意外发生,请添加静态存储说明符,否则只需使用堆。
This will allocate in the BSS segment, which is a part of the heap:
这将在 BSS 段中分配,这是堆的一部分:
static int c[1000000];
int main()
{
cout << "done\n";
return 0;
}
This will allocate in the DATA segment, which is a part of the heap too:
这将在 DATA 段中分配,它也是堆的一部分:
int c[1000000] = {};
int main()
{
cout << "done\n";
return 0;
}
This will allocate at some unspecified location in the heap:
这将在堆中的某个未指定位置分配:
int main()
{
int* c = new int[1000000];
cout << "done\n";
return 0;
}
回答by RSFalcon7
Also, if you are running in most UNIX & Linux systems you can temporarily increase the stack size by the following command:
此外,如果您在大多数 UNIX 和 Linux 系统中运行,您可以通过以下命令临时增加堆栈大小:
ulimit -s unlimited
But be careful, memory is a limited resource and with great power come great responsibilities :)
但要小心,内存是一种有限的资源,能力越大,责任越大:)
回答by Narek
回答by rerun
You array is being allocated on the stack in this case attempt to allocate an array of the same size using alloc.
在这种情况下,您的数组正在堆栈上分配尝试使用 alloc 分配相同大小的数组。