10

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?

pinkpanther
  • 4,770
  • 2
  • 38
  • 62
Aniruddh Jammoria
  • 101
  • 1
  • 1
  • 3
  • 1
    For Linux/gcc see http://stackoverflow.com/questions/2279052/increase-stack-size-in-linux-with-setrlimit, for Windows/gcc, see http://stackoverflow.com/questions/156510/increase-stack-size-on-windows-gcc and for MSVC, http://msdn.microsoft.com/en-us/library/tdkhxaks%28v=vs.110%29.aspx - see there. – FrankH. Jun 10 '13 at 17:40
  • 3
    I don't suppose you can use `std::vector ar(4200000);` ? The rest uses just regular array vernacular for indexing, etc. – WhozCraig Jun 10 '13 at 17:42
  • 3
    It is entirely unclear why this array needs to be on the stack. It will work just as well when you allocate it from the heap. – Hans Passant Jun 10 '13 at 17:44
  • For Visual Studio: [visual studio - Increase stack size in c++ - Stack Overflow](https://stackoverflow.com/questions/40157847/increase-stack-size-in-c) – user202729 Jan 10 '21 at 06:15

4 Answers4

11

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.

MobA11y
  • 18,425
  • 3
  • 49
  • 76
  • This is a very nice answer! Using the first method, is there any fast or default way to initialize all to zero? – xiaohuamao May 29 '19 at 23:45
  • Making it a global variable is another option, but global variable is usually considered bad practice. – user202729 Jan 10 '21 at 06:20
  • @xiaohuamao That should be asked in a new question; but there's one already [c++ - Allocated array already zeroed - Stack Overflow](https://stackoverflow.com/questions/13593165/allocated-array-already-zeroed) – user202729 Jan 10 '21 at 06:21
10

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)

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • 1
    +1 for The last sentence! Please, for the love of god, if you don't understand why the new or vector based solutions are just as good as statically allocated arrays, just use them blindly, and forget this post ever existed! – MobA11y Jun 10 '13 at 17:58
4

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.

Damon
  • 67,688
  • 20
  • 135
  • 185
  • Intellectual curiosity, I'd like to see this method. If you'd like to not provide it here(so as to not give someone a tool they aren't ready to use) you can start a chat with me! Or just send a google phrase that points me in the right direction. – MobA11y Jun 10 '13 at 17:53
  • Ah, compiler flags... that's cheating! :) Although, I think if you're using threading you could actually do this in code as well... – MobA11y Jun 10 '13 at 17:56
  • @ChrisCM: Yes, if you create a thread for just that, you can do it purely programmatically... but it is simply insane compared to using `operator new[]`, which was just _made_ for that kind of task. – Damon Jun 10 '13 at 17:59
0

Are there any reasons you want this on the stack specifically?

I'm asking because the following will give you a construct that can be used in a similar way (especially accessing values using array[index]), but it is a lot less limited in size (total max size depending on 32bit/64bit memory model and available memory (RAM and swap memory)) because it is allocated from the heap.

int arraysize= 4200000;
int *heaparray= new int[arraysize];

... 

k= heaparray[456];

...

delete [] heaparray;

return;
Nicholaz
  • 1,419
  • 9
  • 11
  • I would switch available ram to available memory, as page file/swap space should be counted as well. – MobA11y Jun 10 '13 at 17:46
  • 2
    @MM I was just going the usual way like everybody here: Make a whimsical answer quick, then edit it making comments, because I've too often ended up making a good answer right away and getting three quick answers queued in front. – Nicholaz Jun 10 '13 at 17:46