Matrix segmentation failure

0

I'm getting the segmentation fault error in the code below:

#include <stdio.h>
#include <stdlib.h>

#define linha 1000
#define coluna 1000


int main() {

  long double M1[1000][1000];
  long double M2[1000][1000];
  long double matrizResultante[1000][1000];

    /** multiplicando a matriz **/
    for(int i = 0; i < linha; i++) {
        for(int j = 0; j < coluna; j++) {
            matrizResultante[i][j] = 0;
            for(int k = 0; k < coluna; k++) {
                matrizResultante[i][j] += M1[i][k] * M2[k][j];
            }
        }
    }

  return 0;
}
The purpose of this code is to analyze the access rates to cache memory, my suspicion is that the error is being generated by not initializing the array, but because the values are large the initialization is infeasible. Is this really the problem?

I'm using gcc to compile the code.

    
asked by anonymous 24.09.2016 / 22:26

2 answers

1

The cause of the problem is: STACK OVERFLOW .

The solution is to remove the allocation of huge arrays inside the main function, and allocate statically (or dynamically with malloc).

Frankly, I'm amazed at these horrible things, the stack is not meant to be used that way, with these ten-megabyte statements ... do you need very large arrays? Allocate statically, or dynamically with malloc.

One more thing: the universal convention is to declare defines in UPPERCASE.

#include <stdio.h>
#include <stdlib.h>

#define N_LINHAS 1000
#define N_COLUNAS 1000

long double M1[1000][1000];
long double M2[1000][1000];
long double matrizResultante[1000][1000];

int main()
{
   /** multiplicando a matriz **/
   for (int i = 0; i < N_LINHAS; i++)
   {
      for (int j = 0; j < N_COLUNAS; j++)
      {
         matrizResultante[i][j] = 0;
         for (int k = 0; k < N_COLUNAS; k++)
         {
            matrizResultante[i][j] += M1[i][k] * M2[k][j];
         }
      }
   }

   return 0;
}
    
25.09.2016 / 05:06
1

I was perplexed to rotate your code and get the segmentation fault once it looks correct. Well I did not find out the cause of the problem, but I discovered some clues that the central problem lies in the compiler's allocation of memory to values greater than a certain rate, and also figured out how you can fix it.

#include <stdio.h>
#include <stdlib.h>

#define linha 1000
#define coluna 1000


int main() {

  /*alocação de memória */  
  long double **M1 = malloc(sizeof(long double*) * linha);
  long double **M2 = malloc(sizeof(long double*) * linha);
  long double **matrizResultante = malloc(sizeof(long double*) * linha);

  for(int i = 0; i < linha; i++){
      M1[i] = malloc(sizeof(long double) * coluna);
      M2[i] = malloc(sizeof(long double) * coluna);
      matrizResultante[i] = malloc(sizeof(long double) * coluna);
  }

    /** multiplicando a matriz **/
    for(int i = 0; i < linha; i++) {        
        for(int j = 0; j < coluna; j++) {        
            matrizResultante[i][j] = 0;
            for(int k = 0; k < coluna; k++) {
                matrizResultante[i][j] += M1[i][k] * M2[k][j];
            }
        }
    }

  return 0;
}

Well it's important to highlight a few things: First, the allocation is different from the one compiled by the compiler, so in your case you have to make an assessment to see if you can use it, since the results obtained with the grind cache will not be the same. Secondly, I did not remove memory since it would affect the tests and in this case this is irrelevant, since the program ends soon after using the dynamically allocated data.

    
25.09.2016 / 03:31