samedi 21 septembre 2019

Can't orginize input in c++. exited, segmentation fault

int main(){
  freopen("input.txt", "r", stdin); 
  freopen("output.txt", "w", stdout);
  int N;
  long int ** inp;

  scanf("%d",&N);

  inp = (long int **)malloc((4*N)*sizeof(long int));
  for(int i=0; i<4*N;i++){
    inp[i] = (long int *)malloc((N)*sizeof(long int));
    for (int j =0; j<N;j++){
      scanf("%ld",&inp[i][j]);
    }
  }

  Matrix A(N);
  Matrix B(N);
  Matrix C(N);
  Matrix D(N);


  for(int i =0; i<N; i++){
    for(int j=0; j<N; j++){
      A.Matr[i][j]=inp[i][j];
    }
  }

  for(int i =N; i<2*N; i++){
    for(int j=0; j<N; j++){
      B.Matr[i][j]=inp[i][j];
    }
  }

  for(int i =2*N; i<3*N; i++){
    for(int j=0; j<N; j++){
      C.Matr[i][j]=inp[i][j];
    }
  }

  for(int i =3*N; i<4*N; i++){
    for(int j=0; j<N; j++){
      D.Matr[i][j]=inp[i][j];
    }
  }

  A.Display();
  cout << "\n";
  B.Display();



  return 0;
}

I did matrix OOP class and it works correctly, I'm trying to read the data to work with it. I've got a problem with reading data from the file. On the first line, I've got N which is the size of the matrix, then I have 4*N lines which describes 4 (NxN) matrixes. input.txt looks like this:

2
1 2
2 1
1 0
0 1
5 6
3 -2
1 2
3 4

After running the code I've got this error: "exited, segmentation fault" and I have no idea how to fix it. I guess something wrong with memory allocation and using old functions from C but I don't know what exactly wrong with it.

Aucun commentaire:

Enregistrer un commentaire