Declare a large array on the stack

I am using Dev C ++ to write a simulation program. To do this, I need to declare a one-dimensional array with a double data type. It contains elements 4200000 - as double n[4200000] .

The compiler does not detect errors, but the program terminates execution. I checked and the program runs just fine for an array having elements of 5000 .

Now I know that declaring such a large array on the stack is not recommended. However, the fact is that for modeling it is required that I call certain elements from the array several times - for example, I may need the value n[234] or n[46664] for this calculation. Therefore, I need an array in which it is easier to sift the elements.

Is there any way to declare this array on the stack?

+4
source share
4 answers

No, no (we will say "reasonable") way to declare this array on the stack. However, you can declare a pointer on the stack and save a bunch of memory in memory.

 double *n = new double[4200000]; 

access to n [234] of this should not be faster than access to n [234] of the array you declared as follows:

 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%) 

What if you optimize with -O3 as fast as an array, and much safer. As with

 double *n = new double[4200000]; 

you will have a memory leak if you don't:

 delete[] n; 

And with exceptions and different things, this is a very dangerous way to do things.

+8
source

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

  -Wl, - stack, 36000000 

It may be too large (I'm not sure if Windows sets an upper limit on the size of the stack). In fact, you should not do this, even if it works. Use dynamic memory allocation as indicated in other answers.

(Strange, I am writing an answer and hoping that it will not be accepted ...: -P)

+7
source

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

There is no justifiable reason why the array should be on the stack.

The overhead associated with the dynamic allocation of a single array is incorrect (you can say β€œzero”), and the smart pointer will safely take care that there is no memory leak if this bothers you. The allocated stack memory is no different from the memory allocated by the heap (except for some caching effects for small objects, but they do not apply here).

In any case, just do not do it.

If you insist that you have to allocate an array on the stack, you first need to reserve 32 megabytes of stack space (preferably a little more). To do this, using Dev-C ++ (which assumes Windows + MingW), you need to either set a reserved stack size for your executable file using compiler flags such as -Wl,--stack,34000000 (this is slightly larger than 32MiB), or create a stream (which allows you to specify the reserved stack size for this stream).
But really, again, just don't do it. There is nothing wrong with dynamically allocating a huge array.

+2
source

Are there any reasons you want to do this on the stack?

I ask because the following will give you a construct that can be used in a similar way (especially accessing values ​​with array[index] ), but it is much less limited in size (total maximum size depending on 32 bit / 64 bit model memory 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; 
0
source

Source: https://habr.com/ru/post/1485483/


All Articles