-1

This code is working perfectly with an array size 200. Any bigger than that and it gives a runtime error. But why is it giving a runtime error? Is it because I don't have enough memory or something else ?

This is the code:

void matrix_multip(int matrix1[2500][2500],int matrix2[2500][2500],int n)
{
    int i,j,resultmatrix[2500][2500],k;

    for(i=0;i<n;i++)
    {
       for(j=0;j<n;j++)
       {
           resultmatrix[i][j]=0;

           for(k=0;k<n;k++)
           {
               resultmatrix[i][j]=resultmatrix[i][j]+matrix1[i][k]*matrix2[k][j];
           }
        }
    }
}

int main()
{
  int matrix1[2500][2500],matrix2[2500][2500],n=100,i,j,k;

  for(k=0;k<12;k++)
  {
      for(i=0;i<n;i++)
      {
          for(j=0;j<n;j++)
          {
              matrix1[i][j]=rand();
              matrix2[i][j]=rand();
          }
      }

      clock_t start=clock();

      matrix_multip(matrix1,matrix2,n);

      clock_t end=clock();

      double seconds=(double)(end-start)/CLOCKS_PER_SEC;

      printf("matrix multiplacition for %dx%d took %lf seconds \n ",n,n,seconds);
      n=n+200;
  }

  return 0;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142

3 Answers3

1

Given a 32-bit int the local variables int xxx [2500][2500]; each will need 25 MBytes on the stack, there are 3 of them so 75 Mbytes breaks the stack.

First step is to declare the arrays as static global variables, moving them out of the functions, for example

int resultmatrix[2500][2500];
int matrix1[2500][2500];
int matrix2[2500][2500];

Next step could be to allocate memory dynamically with malloc(). There are two ways to do this.

First as a 1-dimensional array that you index as 2-D by calculating the array offset.

int *matrix1 = malloc(2500*2500*sizeof(int));
...
    matrix1[j*2500+i] = rand();

Secondly to make a 2-D array by allocating memory for an array of row pointers, and for each, allocate memory for each row. But we are straying outside the question here...

int **matrix1 = malloc(2500*sizeof(int*));      // note size is pointer size
for (int i=0; i<2500; i++)
    matrix1[i] = malloc(2500*sizeof(int));      // note size is integer size
...
    matrix1[j][i] = rand();

I leave you to free() the allocated memory.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • using global variables worked but ı wonder whats the difference global variables and local variables space in memory – Lutfiye kaya Mar 01 '15 at 21:28
  • @Lutfiyekaya local variables go on the stack, memory obtained by `malloc()` is from the heap, and global variables do not have an easy answer - try this SO question. http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c – Weather Vane Mar 01 '15 at 21:34
0

You try to allocate 2500 times 2500 times sizeof(int) bytes of memory. That does look like a memory problem. You do it right at the beginning, and it doesn't matter that it's not initialized yet.

Michał Szydłowski
  • 3,261
  • 5
  • 33
  • 55
  • at first i did want to create the array dynamicly but ı get the error "subscripted value is neither array nor pointer nor vector" then ı chage it like this – Lutfiye kaya Feb 28 '15 at 16:53
0

Given the size of the 2D arrays, you are most likely overflowing your stack.

If you define those 2D arrays to be global variables (currently defined in main), there is a good chance (this depends upon your system) that you will not be getting the (same) runtime error.

Hope this helps

Sparky
  • 13,505
  • 4
  • 26
  • 27