Segmentation fault upon assigning value to 3rd matrix

// code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
  1 #include <iostream>
  2 
  3 int main()
  4 {
  5     const int m = 1000;
  6     int a [m] [m];
  7     for (int i=0; i < m; i++)
  8         for (int j=0; j < m; j++)
  9             a [i] [j] = 0;
 10 
 11     int b [m] [m];
 12     for (int i=0; i < m; i++)
 13         for (int j=0; j < m; j++)
 14             b [i] [j] = 0;
 15 
 16     int c [m] [m];
 17     for (int i=0; i < m; i++)
 18         for (int j=0; j < m; j++)
 19             c [i] [j] = 0;
 20 
 21     return 0;
 22 }

the above code gives
Segmentation fault (core dumped)

the program exits normally if
1) comment line 19 or replace it with ';' OR
2) line 5 'const int m = 100;'

if memory gets allocated on line 16, why seg fault upon accessing/writing the location.
Last edited on
How large is a, b and c?

Where are you allocating that memory? Do you think there's enough space there?
around 3.8 MB each. And system has enough memory

sorry for typo, not line 16 but replacing line 19 with ';' (null statement), it exit normally ie if not accessing memory location of third matrix 'c';

it is allocating memory at line 16;

but upon accessing it on line 19, gives seg fault;
closed account (z05DSL3A)
spskhokhar wrote:
around 3.8 MB each. And system has enough memory
...but does main have enough stack space for that?
You are right Grey Wolf, the stack limit is 8192 KB.
Thanks
Topic archived. No new replies allowed.