4

I wanted to declare 4 2-D arrays of size 1000*1000(4*10^6*4 bytes considering int) . Initially, when I declared these arrays in main() function, I was getting segmentation fault.

Later I declared the arrays globally and the program worked fine. Why this is so??

vinit
  • 155
  • 14
  • 10
    Welcome to stackoverflow, a place where you can ask questions about *stack overflows*, which is what you got. – Some programmer dude May 20 '14 at 12:56
  • (and please tag your questions with the language you're using) – Mat May 20 '14 at 12:57
  • @Mat Shame that question and its answers are terrible. – Lightness Races in Orbit May 20 '14 at 12:59
  • @LightnessRacesinOrbit: well, there's also http://stackoverflow.com/questions/7902228/segmentation-fault-large-arrays?rq=1 – Mat May 20 '14 at 13:03
  • http://stackoverflow.com/questions/17029671/declare-large-array-on-stack is actually more useful in terms of answers for C++ – Mat May 20 '14 at 13:05
  • 2
    One question that is added by the OP is "Why does not it segfault also when the arrays are declared as global variable?". @vinit I suggest that you edit your question post, to show two small programs showing the behavior you describe: one that segfaults, and one that does not. – lrineau May 20 '14 at 13:06
  • 1
    None of the dups even mention static storage duration, even though that's 50% of this question. – Lightness Races in Orbit May 20 '14 at 13:25
  • @LightnessRacesinOrbit: want to re-open this then? – Mat May 20 '14 at 14:09
  • 1
    @Mat I also agree that the pointed duplicate question does not answer all the question of the OP. That is not a perfect duplicate. I am not a moderator, but as far as I understand the question should be reopened. – lrineau May 20 '14 at 14:15
  • @lrineau: I already voted to reopen, I hadn't really taken that into account when voting to close. (Ping'd Lightness in case they wanted to do the same) – Mat May 20 '14 at 14:17
  • @LightnessRacesinOrbit: should not the question be reopened? – lrineau May 20 '14 at 14:19
  • @Mat: for user who cannot vote open/close, which flag is adapted to ask for reopening? – lrineau May 20 '14 at 14:19
  • @lrineau: there's no pre-made flag for that, you need to chose the "other" reason and explain what you'd like – Mat May 20 '14 at 14:20
  • @Irineau: Yeah I think so. We can always re-close if we find a better duplicate. – Lightness Races in Orbit May 20 '14 at 14:26
  • @Irineau: Thanks for your efforts to reopen the question. I am really obliged :) . – vinit May 21 '14 at 07:51
  • @vinit: In StackOverflow, the self answer should be formatted differently. Please create an official answer to your question, and remove the answer from the question. After a few hours, you will be able to self-accept your own answer, to mark the question as closed. http://stackoverflow.com/help/self-answer – lrineau May 21 '14 at 15:00

1 Answers1

5

When large arrays are declared inside a function, they are stored on the stack frame, whose size is not much(don't know the exact value).

While when the array is declared globally, it is stored in Data (if the variable is initialized) and in bss (if not initialized). The size of Data and bss is very high(don't know the exact value), that is why they could accommodate large array declarations.

vinit
  • 155
  • 14