3

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. Previously, I succeeded declaring 1000 by 2 int array. 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.

Any advice or suggestion? I didn't face this issue in python nor in matlab. I am using Microsoft Visual Studio 2010.

2
  • 1
    If you can't troubleshoot it sitting in front of the actual code, with your entire working environment at your disposal, what hope is there of us figuring it out with no tools and no visibility? It is a memory related issue; you're going to have to do some debugging and figure out why. Commented Jul 11, 2013 at 18:31
  • Try making 1024 separate 1024 size float array allocations if your 2D array does not need to be contiguous. Commented Jul 11, 2013 at 18:35

2 Answers 2

13

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

The reserve value specifies the total stack allocation in virtual memory. For x86 and x64 machines, the default stack size is 1 MB. On the Itanium chipset, the default size is 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.

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Or just use a vector, it will do all of that for you, vector<vector<float>> will allocate it's data on the heap and free it automatically with RAII
However, note that the same applies to C programs (except using malloc/free instead of new/delete) so the answer is more generally useful by not using vectors.
2

Are you declaring this on the stack perhaps? Objects that big have to be on the heap!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.