0

I am developing a code in which there is need for declaring array of double of size 150000 and when one array is declared code is running successfully.if we declare two arrays then while execution it terminates throwing exception.

Code is :

double a[150000];
double b[150000];

if we declare a only then it executes perfectly.if declare both a and b then it terminates. Can anyone suggest how to resolve this?

6
  • 5
    Stackoverflow, right? Give the arrays static storage or use a std::vector. Commented Apr 25, 2013 at 11:20
  • If you declare these arrays locally in a function, they are over 2MB of stack space. On Windows the default stack size is only 1MB. Commented Apr 25, 2013 at 11:21
  • 1
    Assuming these indeed are local variables, the space needed is 2 * 8 * 150000 (on x86), which is around 2.2 MB. Many operating systems limit the maximum stack of a process to less than that. Commented Apr 25, 2013 at 11:21
  • Make them float arrays. Problem solved! Commented Apr 25, 2013 at 11:23
  • 2
    @HotLicks - problem solved, but new problem introduced! Commented Apr 25, 2013 at 11:33

3 Answers 3

6

The two arrays are overflowing the stack (assuming they are local variables). Dynamically allocate memory for the arrays instead, using a std::vector to manage the memory for you:

std::vector<double> a(150000);
std::vector<double> b(150000);

Even though the std::vector instances are on the stack, the std::vector dynamically allocates memory internally for the data which is on the heap, avoiding the stack overflow.

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

2 Comments

This is good, but worth mentioning that in C++03 it's unavoidably slower initially as all elements are written to when the vector is created.
@TonyD: That is just 2M of data, the cost of initialization is most probably negligible. If that is an issue, there are alternatives, but most probably the arrays are meant to be used, in which case most of the cost of initialization (actually touching the memory addresses) have to be paid anyways...
4

Okay! You have Stack Overflow in your app! enter image description here

Fixing examples:

  • don't use stack - use dynamic memory allocation (heap):

    double* a = new double[150000];

  • use STL container, for example, vector - internally it allocates things on heap

    std::vector<double> a(150000);

  • increase stack size (bad idea, but if you really need it read you compiler docs, and look here)

  • redesign you code somehow

7 Comments

Don't use dynamic memory allocation like that. Use <vector> or at least make_shared or make_unique.
@Bartek Banachewicz Well, <vector> is ok, but <unique_ptr> and <shared_ptr> is completly wrong here: they will leak, because of using delete operator instead of delete []
You can safely use unique_ptr<T[]>, and it will call delete[]. You might want to update your knowledge about smart pointers.
@Bartek Banachewicz Whoops! Looks like my ancient brain is a little out of date. So <unique_ptr> have template specialization for arrays. And what about <shared_ptr>?
for shared_ptr you need your own deleter. Still no leak.
|
2

There is one solution to this problem, but it leads to (at least) three different follow-on solutions. The solution is "don't use large arrays as local variables, because it blows up the stack".

The solution clearly means changing the code in some way. There are a few different ways to do that.

The obvious and straight-forwards solution is to use std::vector<double> instead.

Another solution is to use `

unique_ptr<double[]> a = std::unique_ptr<double[]>(new double[150000]);

The third, and SOMETIMES a good solutions, is to make a and b global variables.

There are several other variants, but they are generally variations on the same theme, just with slight variations. What is best in your case really depends on what the rest of your code is doing. I'd start with std::vector<double>, but other alternatives do exist, should that be an unsuitable solution for some reason.

3 Comments

<unique_ptr> will leak, because of using delete operator instead of delete []
I've just edited in the missing space, which shows the extra [] that I believe will solve that problem, no?
Forget it =) 5 min ago I find out that <unique_ptr> has template specialization for arrays, with right deleter. Nice, huh?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.