简体   繁体   English

声明大数组时堆栈/堆溢出

[英]Stack/heap overflow when declaring a large array

I was trying to declare a 1024 by 1024 float array but a window just popped up saying project_name.exe has stopped working... with options whether to debug or close program. 我试图声明一个1024 x 1024的float数组,但是刚刚弹出一个窗口,说project_name.exe已停止工作...带有调试或关闭程序的选项。 Previously, I succeeded declaring 1000 by 2 int array. 以前,我成功声明了1000 by 2 int数组。 I've kind of searched the internet for possible cause, and they said its memory related issue, "stack/heap overflow" in exact. 我已经在互联网上搜索了可能的原因,他们说与内存有关的问题,确切地说是“堆栈/堆溢出”。 They said that it is even worse for the case of float. 他们说,对于浮标情况更糟。

I only need up to 5 or 6 decimal places. 我只需要最多5或6个小数位。

Any advice or suggestion? 有什么建议或建议吗? I didn't face this issue in python nor in matlab. 我在python和matlab中都没有遇到这个问题。 I am using Microsoft Visual Studio 2010. 我正在使用Microsoft Visual Studio 2010。

Are you declaring this as a local variable in a function or method? 您是在函数或方法中将其声明为局部变量吗? If so it's a classic stack overflow. 如果是这样,这是经典的堆栈溢出。 For VS2010 see http://msdn.microsoft.com/en-us/library/8cxs58a6%28v=vs.100%29.aspx 对于VS2010,请参见http://msdn.microsoft.com/zh-cn/library/8cxs58a6%28v=vs.100%29.aspx

The reserve value specifies the total stack allocation in virtual memory. 保留值指定虚拟内存中的总堆栈分配。 For x86 and x64 machines, the default stack size is 1 MB. 对于x86和x64机器,默认堆栈大小为1 MB。 On the Itanium chipset, the default size is 4 MB. 在Itanium芯片组上,默认大小为4 MB。

So a 1024x1024 array of floats (assuming 4 bytes per float) clocks in at a whopping 4mb - you've sailed right through the default stack limit here. 因此,一个1024x1024的float数组(假设每个float包含4个字节)以高达4mb的速度进入-您已经在这里通过了默认的堆栈限制。

Note that even if you do have an Itanium you're not going to be able to use all of that 4mb - parameters, for example, will also need to be stored on the stack, see http://www.csee.umbc.edu/~chang/cs313.s02/stack.shtml 请注意,即使您确实拥有Itanium,也将无法使用所有这4mb-例如,参数也需要存储在堆栈中,请参见http://www.csee.umbc。 edu /〜chang / cs313.s02 / stack.shtml

Now, you could just increase the stack size, but some day you're going to need to use a larger array, so that's a war of attrition you're not going to win. 现在,您可以增加堆栈大小,但是总有一天您将需要使用更大的数组,所以这是一场消耗不了的战争。 This is a problem that's best solved by making it go away; 要解决这个问题,最好先解决它。 in other words instead of: 换句话说,而不是:

float stuff[1024 * 1024];

You declare it as: 您将其声明为:

float *stuff = new float[1024 * 1024];
// do something interesting and useful with stuff
delete[] stuff;

Instead of being on the stack this will now be allocated on the heap. 现在,它不再位于堆栈中,而是分配到堆中。 Note that this is not the same heap as that mentioned by Robert Harvey in his answer; 请注意,这是一样的堆作为由罗伯特·哈维在他的回答中提到; you don't have the limitations of the /HEAP option here. 您在这里没有/ HEAP选项的限制。

Are you declaring this on the stack perhaps? 您是在堆栈上声明吗? Objects that big have to be on the heap! 这么大的对象必须在堆上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM