Failure of matrix segmentation

Asked

Viewed 477 times

0

I’m getting 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 matrix, but as the values are large the initialization is unviable. Is that really the problem?

I’m using gcc to compile the code.

  • 2

    There is no reason not to work, and it even worked (I reduced the size so as not to over-time the ideone): http://ideone.com/O2n3aE The code does not seem to help what you want.

  • moustache all right? very strange here when I try to compile it throws me a segmentation error, I’m using gcc. When you speak in the code does not seem to help in what I wish, this talking about the issue of accessing memory cache? my goal is to try to analyze the rate of miss and hit, by the size of the matrix I think should help me in this proposal.

  • Happens seg fault when compiling, the problem is GCC :P I talk about it. You can even use this, but the experiment needs to be much more controlled than just running it. But that was more of an opinion, I can’t even talk without knowing everything you’re doing.

  • bigown because it is the problem this in gcc, I am running with codeblocks and apparently as you said this all right, about the experiment I am using the Valgrind, my goal is to try to improve the code, decrease the miss rates.

  • The value that is going to this matrix may be getting higher than that supported by long double. I suggest you place a printf to identify at which point of iteration the segmentation failure occurs.

2 answers

1

I was perplexed to run your code and receive the segmentation failure since it seems correct. Well I didn’t find out the cause of the problem, but I did find some clues that the central problem lies in the memory allocation made by the compiler for 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;
}

Good is important to highlight some things: First, the allocation is different from the one performed 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 equal. Second did not shift memory since it would affect the tests and in this case this is irrelevant, since the program ends soon after the use donates dynamically allocated data.

  • Opa alright Gabriel, I also got like this when the segmentation error occurred, as you commented seems to be something related in the memory allocation, the most interesting is that even changing the values from long double to int still the error persists, the possible solution is to decrease the size of the row and column, as I would not like to do this I’m still looking for another solution, on your solution I think I can not use because the results obtained affected the analysis.

  • 1

    I see, good anyway if I can find another solution I put here.

1


The cause of the problem is: STACK OVERFLOW.

The solution is: take out the allocation of the huge arrays within the main function, and allocate statically (or dynamically with malloc).

Frankly, I am amazed by these horrible things, the stack is not to be used this way, with these statements of tens of megabytes...need very large arrays ? Allot statically, or dynamically with malloc.

One more thing: the universal convention is to declare 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.