0

I have a C++ object in some part of my code like this:

class Object

{
public : 
float a, b, c;
}

When I create a 2 dimensional array like this, it works fine :

Object myArray [500][500];

When I make it like this, I get segmentation fault :

Object myArray [1000][1000];

What causes that segmentation fault, how can I get rid of it? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198

3 Answers3

2

To create a huge array, you need to make it global. Local variables have a size limit. Another solution is to allocate the array dynamically.

Elias Kosunen
  • 433
  • 7
  • 20
2

Stack has a limited size, usually 1 to 10 MB on a modern machine. You array takes at least 16 MB.

The best case for this would be to allocate the array on the heap.

2501
  • 25,460
  • 4
  • 47
  • 87
1

Most likely you have exceeded the stack size so you either need to use dynamic arrays(hence heap) or increase the stack size in the compiler settings.

ixSci
  • 13,100
  • 5
  • 45
  • 79