Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare large array on Stack

I am using Dev C++ to write a simulation program. For it, I need to declare a single dimensional array with the data type double. It contains 4200000 elements - like double n[4200000].

The compiler shows no error, but the program exits on execution. I have checked, and the program executes just fine for an array having 5000 elements.

Now, I know that declaring such a large array on the stack is not recommended. However, the thing is that the simulation requires me to call specific elements from the array multiple times - for example, I might need the value of n[234] or n[46664] for a given calculation. Therefore, I need an array in which it is easier to sift through elements.

Is there a way I can declare this array on the stack?

like image 964
Aniruddh Jammoria Avatar asked Jun 10 '13 17:06

Aniruddh Jammoria


3 Answers

You can increase your stack size. Try adding these options to your link flags:

-Wl,--stack,36000000

It might be too large though (I'm not sure if Windows places an upper limit on stack size.) In reality though, you shouldn't do that even if it works. Use dynamic memory allocation, as pointed out in the other answers.

(Weird, writing an answer and hoping it won't get accepted... :-P)

like image 97
Nikos C. Avatar answered Oct 22 '22 05:10

Nikos C.


Yes, you can declare this array on the stack (with a little extra work), but it is not wise.

There is no justifiable reason why the array has to live on the stack.

The overhead of dynamically allocating a single array once is neglegible (you could say "zero"), and a smart pointer will safely take care of not leaking memory, if that is your concern.
Stack allocated memory is not in any way different from heap allocated memory (apart from some caching effects for small objects, but these do not apply here).

Insofar, just don't do it.

If you insist that you must allocate the array on the stack, you will need to reserve 32 megabytes of stack space first (preferrably a bit more). For that, using Dev-C++ (which presumes Windows+MingW) you will either need to set the reserved stack size for your executable using compiler flags such as -Wl,--stack,34000000 (this reserves somewhat more than 32MiB), or create a thread (which lets you specify a reserved stack size for that thread).
But really, again, just don't do that. There's nothing wrong with allocating a huge array dynamically.

like image 28
Damon Avatar answered Oct 22 '22 03:10

Damon


No there is no(we'll say "reasonable") way to declare this array on the stack. You can however declare the pointer on the stack, and set aside a bit of memory on the heap.

double *n = new double[4200000];

accessing n[234] of this, should be no quicker than accessing n[234] of an array that you declared like this:

double n[500];

Or even better, you could use vectors

std::vector<int> someElements(4200000);
someElements[234];//Is equally fast as our n[234] from other examples, if you optimize (-O3) and the difference on small programs is negligible if you don't(+5%)

Which if you optimize with -O3, is just as fast as an array, and much safer. As with the

double *n = new double[4200000]; 

solution you will leak memory unless you do this:

delete[] n;

And with exceptions and various things, this is a very unsafe way of doing things.

like image 10
ChrisCM Avatar answered Oct 22 '22 05:10

ChrisCM