2

When trying to define 5 1d arrays, the following code gives me an unexpected return value:

#define N 100000
int main()
{
   double a1[N];
   double a2[N];
   double a3[N];
   double a4[N];
   double a5[N];
   return 0;
}

If a swich N to be 10, 100, 1000 or even 10000 it works just fine. Can someone explain to me what is going on?

Thanks in advance

sruizbon
  • 21
  • 1
  • 2
    Local arrays are allocated on stack. Stack is limited. – Eugene Sh. Oct 24 '18 at 17:58
  • Can you show the error/return value you're getting? Although I suspect this is a stack overflow. – CoffeeTableEspresso Oct 24 '18 at 18:06
  • i get -1.073.741.571 I solved it declaring it globally as suggested in the post above – sruizbon Oct 24 '18 at 18:14
  • 1
    I'm not sure if this is a duplicate. OP asked for an explanation of what's going on, _not_ a way to declare and use a huge array in C. In my mind, a good answer to this question would explain a way for OP to see what stack size was used, showing him that the error is from a stack overflow, then mention workarounds (not declaring the array on the stack). – CoffeeTableEspresso Oct 24 '18 at 18:16
  • Finish your drinks, everyone. It's a Stack Overflow – Tim Randall Oct 24 '18 at 18:30

0 Answers0