-1

When I execute this code (gcc compiled):

#include <stdio.h>

int main() {

    int table[1005][1005];
    return 0;
}

it stops working, but when I change it to:

#include <stdio.h>

int table[1005][1005];

int main() {

    return 0;
}

it works just fine.. Why is this concretely happening? Does global variables get more space to allocate? Why?

yat0
  • 967
  • 1
  • 13
  • 29
  • Since your program does nothing but return 0, I think you need to define what "stops working" means. – Daniel Feb 11 '15 at 01:35
  • I just execute it after compile it, that's all. It seems that it can't allocate the multidimensional-array when it is inside main. @Daniel – yat0 Feb 11 '15 at 01:35
  • 1
    Possible duplicate of [Why does a large local array crash my program?](http://www.stackoverflow.com/questions/22945647/why-does-a-large-local-array-crash-my-program) – Spikatrix Feb 11 '15 at 02:22
  • 1
    the stack is only so big. Most likely that 1005*1005*sizeof(int) is exceeding the available stack size – user3629249 Feb 11 '15 at 02:36

1 Answers1

2

First way is probably creating the array on the stack, the second is probably putting it into the "data segment".

The amount allocated may be too big for the stack depending on your platform.

John3136
  • 28,809
  • 4
  • 51
  • 69