1

When I run the following code I get a stack overflow. How is it possible? I thought if I define an array outside of main it will be static and I won't have memory problems? What can I do against it?

#include <stdio.h>

#define dim1 14001
#define dim2 14001
#define dim4 8

double large_array[dim1][dim2][dim4];

int main()
{
  large_array[30][6][5] = 1337;
  printf("%lf\t",large_array[30][6][5]);
}
10
  • 11
    Does your machine have the 12 GB memory that this array would require? Commented Mar 11, 2015 at 9:55
  • @amchacon No, it's ~12 GB. A double is 8 bytes long Commented Mar 11, 2015 at 10:04
  • @amchacon: Do the math again. And additionally, remember that double is bigger than one byte. Commented Mar 11, 2015 at 10:04
  • 1
    @Hurkyl: "static" has several meanings. This has static storage duration. Commented Mar 11, 2015 at 10:06
  • 1
    12Gb memory required for this. You'll need to (a) compile it as a 64-bit application and (b) either buy more physical memory (fast) or mmap it and page in the parts you need as you need them (slow). Alternatively rent yourself some time on AWS x.large compute node at $0.60/hr :) Commented Mar 11, 2015 at 10:22

3 Answers 3

2

One problem this has is that it's very likely that the place where the compiler wants to store the array doesn't have room to fit that much memory -- in some environments, very large allocations must be done on the heap, e.g. with malloc, new, or other similar means (or implicitly through those means, e.g. as done internally by std::vector).

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

10 Comments

could you maybe give ma a very simple example?
How malloc,new is different from that global array.i.e..both are globally stored, so does it really make sense.
Actually, I figured out it works when I use malloc. But I need a for-loop, which I cannot do outside main.
@Jerry: I'm unsure on the fine details, but I can confirm the end result from experience. I think it has something to do with how executables get loaded, with the array going in the data segment rather than on the heap or somesuch.
@Hurkyl: could you please provide a minimal example of that?
|
0

3-D global array you declared consumes too much memory (~1043456KB.i.e..~1GB) on heap while initialising at compile time, which is why your program is giving overflow problem.
One intuitive way to handle this problem is to use multidimensional map STL instead of Multidimensional Global array.

#include<stdio.h>
#include<map>
/*#define dim1 14001
#define dim2 14001
#define dim4 8
double large_array[dim1][dim2][dim4];
*/
using namespace std;
map<double, map<double,map<double,double> > > large_array;
int main()
{
  large_array[30][6][5] = 1337;
  printf("%lf\t",large_array[30][6][5]);
}

2 Comments

@ Jerry: great, that works, thanks! so how much memory is allocated to this array if I don't even give the dimensions in advance?
It's not an array.it is hash map.you don't have to specify any size.see this cplusplus.com/reference/map/map
0

The maximum size of an statically allocated array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details and compiler implementation choices.

As interjay said in comment, your allocation requires quite higher amount of space.

In C++, you should use the provided containers like vector or the boost multi dimensional arrays to handle such cases.

8 Comments

thanks, I understand. Is there a way to solve this besides buying a new computer?
In windows, it's 2gb.
@Holger Compile in 64 bits.
"The actual limit may be less, depending on operating system implementation details." - there are also compiler implementation choices to consider... the C++11 Standard's recommended minimum object size to support is only 262 144 bytes (from Annex B). Specific implementations are required to document their actual support.
@Holger Do you set a non-zero value to the vast majority of elements in your array(s)? If most are zero, then you could look into sparse data structures which exploit the "zero-ness" of your data, e.g. libraries for sparse matrices.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.