-2

I am trying to write a program for adding 2 matrices.

If I increase the matrix size I get the following error:

Segmentation fault (core dumped)

Here is my code:

#include <iostream>

using namespace std;
int main()
{
  int thrd ;
  int m = 1000 ;
  int n = 1000 ;
  int a[m][n] ;
  int b[m][n] ;
  int c[m][n] ;
  for(int i=0 ; i<m ; i++)
  for(int j=0 ; j<n ; j++)
        {
         a[i][j] = 0 ;
         b[i][j] = 0 ;
        }

  for(int i =0 ; i<m ; i++)
    {
      for(int j=0 ; j<n ; j++)
        c[i][j] = a[i][j] + b[i][j] ;
    }
  return 0 ;
}

So what should I do?

I'd appreciate any help on this error.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
hanie
  • 3
  • 2
  • 3
    ~11 MB *may* be more than your stack can handle. – EOF Aug 22 '15 at 14:08
  • First step is to fire up the debugger, step through your code and find the exact line of code where this happens. – πάντα ῥεῖ Aug 22 '15 at 14:08
  • @EOF how should you determine that the space I require is 11 MB ? – hanie Aug 22 '15 at 14:19
  • @user2944588: Do you need help with multiplication? You have `3` arrays of `1000` arrays of `1000` `int`s, each taking `sizeof(int)` bytes. Assuming `sizeof(int) == 4`, you get `3 * 1000 * 1000 * 4`. Stack size is implementation-dependent, but more than ~8 MB would be rather large. – EOF Aug 22 '15 at 14:44

2 Answers2

0

It looks like you're exhausting stack memory with such big matrices. I suggest static allocation instead, i.e., declare a, b and c as global:

#define M 1000
#define N 1000

static int a[M][N];
static int b[M][N];
static int c[M][N];

int main() {
    /* ... */
}

Note that M and N can't be variables if you do this.

Alternatively, you can try dynamic allocation.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • thanks a lot.but in this way is the speed of program drop off ? how should i know how much stack memory i have for my program ? and can i resize it ? – hanie Aug 22 '15 at 14:27
  • @user2944588 You shouldn't see a difference in execution time. The stack size depends on your operating system, limits configuration, and other factors. You can see the stack size limit on the command line with `ulimit -a` (see the line *stack size*). You can resize it; there are numerous resources on the web explaining how to do so. See for example http://stackoverflow.com/questions/2279052/increase-stack-size-in-linux-with-setrlimit – Filipe Gonçalves Aug 22 '15 at 14:45
0

Declare these matrices globally as shown below and your problem will be solved. This is generally because global variables are given memory from heap while variables in main are given memory from stack.

#include <iostream>
#define max 1000
int a[max][max] ;
int b[max][max] ;
int c[max][max] ;

using namespace std;
int main()
{
  int thrd ;
  int m = 1000 ;
  int n = 1000 ;
  for(int i=0 ; i<m ; i++)
  for(int j=0 ; j<n ; j++)
        {
         a[i][j] = 0 ;
         b[i][j] = 0 ;
        }

  for(int i =0 ; i<m ; i++)
    {
      for(int j=0 ; j<n ; j++)
        c[i][j] = a[i][j] + b[i][j] ;
    }
  return 0 ;
}

Hope this helps you.

dazzieta
  • 662
  • 4
  • 20