declaring arrays inside main causes segmentation fault

I ran into an issue while working on a coding problem. My code was seg faulting and it started to work when I declared my arrays outside the int main(){} function. Can someone explain why the seg faulting code below causes an error?
seg faulting code
1
2
3
4
5
6
7
int main() {
   int N, H, I, i, j, temp1,temp2;
   int a[1900][1900];
   int ta[1900][1900];
   int m[1900];
   ...
   ...


Working code
1
2
3
4
5
6
7
8
9
int a[1900][1900];
int ta[1900][1900];
int m[1900];

int main() {
   int N, H, I, i, j, temp1,temp2;
   ...
   ...


Your time and help is appreciated.
Last edited on
The arrays are large and you were running out of space for variables with automatic storage duration (stack space).

Declare the large arrays with static storage duration and things would be fine. (The static data area is typically larger than the area available for objects with automatic storage duration.)

1
2
3
4
5
6
7
int main() {
   int N, H, I, i, j, temp1,temp2;
   static int a[1900][1900];
   static int ta[1900][1900];
   int m[1900];
   ...
   ...
Wow, thanks for that info, I did not know that.

Is the static storage area, as explained above, store the variables in different memory segment. Is it still part of stack?

And does declaring variables outside main default them to static declaration of the variables? Or, in other words, is it same as declaring the variable as "static int", when we declare variable outside of main?
> Is the static storage area, as explained above, store the variables in different memory segment.
> Is it still part of stack?

As far as the C++ standard goes, 'storage duration' is an abstract concept. In simple terms, the standard is only concerned about the lifetime of an object (and the duration for which the storage for an object would be available).

For an object with static storage duration, the storage for the object is allocated when the program begins and deallocated only when the program ends. The amount of storage required for all the objects with static storage duration in a program can be determined statically, at build time; this is a fixed amount of storage which is present from the beginning, till the program ends. In most implementations, this storage is in one or more pre-allocated data sections.

For objcts with automatic storage duration, the storage for the object is required only from the beginning of the enclosing code block and this storage can be deallocated on exiting from the code block. Most implementation use the run-time stack for this kind of reclaimable storage.


> And does declaring variables outside main default them to static declaration of the variables?
> Or, in other words, is it same as declaring the variable as "static int", when we declare variable outside of main?

Yes.
Every object declared at namespace scope has a static storage duration.
Every object declared at local scope has an automatic storage duration (unless it was declared static or extern).

In the above example, the variables declared outside main() are at namespace scope (the global namespace) and have static storage duration. The variables declared inside the block scope in main() would have static storage duration only if they were declared with the keyword static.
Last edited on
Thanks @JLBorges. That was very helpful
Topic archived. No new replies allowed.