1

I want a big sized array in a program.

My code has a structure which contains 3 float type variables and I want a largely sized 3 dimensional arrays of this structure.
My code is giving the output for small sized arrays, but when I increase the size of the array, I get a stack overflow exception/error.

Please help.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

4 Answers4

3

Instead of

int main() {
   float array[N][N][N][N][N];
   ...

write

float array[N][N][N][N][N];

int main() {
   ...

Alternatively, write:

int main() {
   static float array[N][N][N][N][N];
   ...

BTW: my default setup in VC10/Win32 allows stack allocation up to N=12, which means ~ 990KB.

regards

rbo

rubber boots
  • 14,924
  • 5
  • 33
  • 44
  • yes this is working. you can also increase the value of N by changing the memory reserved for stack. Its 1Mb by default. – Karan Arora Jun 13 '12 at 19:24
2

The made-for solution here is std::vector. It allocates memory on the heap, which is much larger than the stack, and can change its size without much extra effort at all.

typedef std::vector<float> vec;
typedef std::vector<vec> vec2D;
typedef std::vector<vec2D> vec3D;
typedef std::vector<vec3D> vec4D;
typedef std::vector<vec4D> vec5D;

vec5D d (15, vec4D (15, vec3D (15, vec2D (15, vec (15)))));
//use as you would a normal array for the most part

Of course having a 5D array (as mentioned "needed" in the comments) isn't something you particularly want. I recommend you make a Matrix class of sorts that has an underlying 1D vector instead.

Even better, rethink your design. There's rarely a time when you actually do need a highly multidimensional array such as that one.

chris
  • 60,560
  • 13
  • 143
  • 205
  • The part about std::array is not true - the memory is not allocated dynamically. – jrok Jun 13 '12 at 17:42
  • @jrok, I could've sworn I've seen/had this discussion, and it was on the heap. I'll have to find it. – chris Jun 13 '12 at 17:43
  • Thanks for the information on the allocating memory on heap. Actually have 1 array of volume elements which is 3D , and a surface element which is 2D , and I need a distance between the 2. – Karan Arora Jun 13 '12 at 17:43
  • @jrok, Ah, yes, that wasn't `std::array`. Let me fix that then. – chris Jun 13 '12 at 17:44
  • And i think 5D array is best suited for it, bcoz i can recall a particular distance easily, BTW now the code is working, i do it by changing the stack size and global allocation – Karan Arora Jun 13 '12 at 17:45
1

In addition to allocating it on the heap, you could simply try to reserve a bigger stack, using /F or /STACK option, or in project properties:

enter image description here

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
0

I don't know if you need to, but if you want, you may change the default stack size with linker options, for example in Visual Studio go "Properties - Configuration Properties - Linker - System - Stack Reserve Size".

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31