在 C++ 中创建一个大数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3137598/
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
Create a big array in C++
提问by Yongwei Xing
Possible Duplicate:
Segmentation fault on large array sizes
可能的重复:
大数组大小的分段错误
Hi all
大家好
I am trying to create a very big array in the VS 2010 using C++.
我正在尝试使用 C++ 在 VS 2010 中创建一个非常大的数组。
When I try to create a array like below
当我尝试创建一个如下所示的数组时
int dp[4501][4501]
or
int dp[1000][1000]
It threw an exception "Stack Overflow" Then I change it to:
它抛出了一个异常“堆栈溢出”然后我将其更改为:
int dp[100][100]
everything is fine.
一切安好。
So if I want to create a big array like above, what should I do?
那么如果我想创建一个像上面这样的大数组,我该怎么办?
Best Regards,
此致,
回答by vpit3833
回答by GManNickG
You should use dynamic allocation:
您应该使用动态分配:
typedef std::vector<int> int_vector;
int_vector dp(10000);
A double array can be simulated by nesting arrays:
可以通过嵌套数组来模拟双数组:
typedef std::vector<int_vector> int_double_vector;
int_double_vector dp(4501, int_vector(4501));
回答by adatapost
Text from Parashift faq : Why should I use container classes rather than simple arrays?
Parashift 常见问题解答中的文本: 为什么我应该使用容器类而不是简单的数组?
EDIT:
编辑:
Take a look at stackoverflow threads:
看看stackoverflow线程:
When would you use an array rather than a vector/string?Why use iterators instead of array indices?
回答by John
Your stack has overflowed with too many bits. You must drain them. Preferably onto a heap of other bits. I suggest /F67108864. The /F stands for "F'ing hell why is the stack so small compared to the heap?". The 67108863 is arbitrary.
您的堆栈溢出了太多位。你必须排干它们。最好放在一堆其他位上。我建议 /F67108864。/F 代表“地狱为什么堆栈比堆小?”。67108863 是任意的。
回答by Donotalo
If you want to avoid new[]
, or avoid using std::vector
, make the array global. This will put the array on heap and stack overflow will not occur.
如果您想避免new[]
或避免使用std::vector
,请将数组设为全局。这会将数组放在堆上,并且不会发生堆栈溢出。
回答by Philipp
Your declaration looks a bit as if dp
will be used as a matrix. In that case, a dedicated (dense) matrix class such as boost::numeric::ublas::matrix
is the simplest solution, easier and more local than a vector of vectors. If the matrix is sparsely populated, use a sparse matrix class instead.
您的声明看起来有点像是dp
将用作矩阵。在这种情况下,专用(密集)矩阵类boost::numeric::ublas::matrix
是最简单的解决方案,比向量的向量更容易和更局部。如果矩阵是稀疏填充的,请改用稀疏矩阵类。
回答by stinky472
So if I want to create a big array like above, what should I do?
那么如果我想创建一个像上面这样的大数组,我该怎么办?
Avoid using the stack for these cases (in other words, avoid creating arrays like these which aren't heap-allocated when working inside a function). Just to give you an idea, my thread-local stack is only 16 kilobytes large. 4501 * 4501 * 4 (assuming 4 bytes per int) = ~81 megabytes.
避免在这些情况下使用堆栈(换句话说,避免创建这样的数组,这些数组在函数内部工作时不是堆分配的)。只是给您一个想法,我的线程本地堆栈只有 16 KB 大。4501 * 4501 * 4(假设每个 int 有 4 个字节)= ~81 兆字节。
Consider something like this instead:
考虑这样的事情:
typedef vector<int> Row;
typedef vector<Row> Matrix;
Matrix dp(4501, Row(4501) );
If you want to create a 10x50 matrix:
如果要创建 10x50 矩阵:
Matrix dp(10, Row(50) );
You can use this just like your normal dp array had it not overflowed the stack. This one will be allocated and automatically deallocated to/from the heap so that you don't have to worry about stack overflow when using it.
如果它没有溢出堆栈,您可以像普通的 dp 数组一样使用它。这个将被分配并自动释放到/从堆中释放,因此您在使用它时不必担心堆栈溢出。
dp[5][10] = 123;
Good luck!
祝你好运!
[Edit] There are also matrix solutions in boost worth looking into but suggesting boost might be a bit premature given the nature of the topic.
[编辑] boost 中还有一些矩阵解决方案值得研究,但鉴于主题的性质,建议 boost 可能有点为时过早。