This question is an exact duplicate:

There is a class for representing a two-dimensional dynamic array. The number of columns and columns is indicated through the constructor, and dynamic memory is allocated there. Next, the array must be filled from the keyboard via an overloaded input statement, but an error occurs: Exception thrown: read access violation. Debugging error

Here is the code itself:

#include <iostream> using std::cout; using std::cin; using std::endl; using std::istream; using std::ostream; class IntArr { friend istream& operator >> (istream &in, IntArr& a) { for (int i = 0; i < a.rows; i++) { for (int j = 0; j < a.cols; j++) { in >> a.pa[i][j]; } } return in; } private: int **pa; int rows, cols; public: IntArr() { pa = 0; rows = 0; cols = 0; } IntArr(int rows, int cols) { this->rows = rows; this->cols = cols; int **pa = new int*[this->rows]; // две строки for (int count = 0; count < this->rows; count++) { pa[count] = new int[this->cols]; // и пять столбцов } } }; int main() { IntArr first(3, 4); cin >> first; system("pause"); return 0; } 

I would be grateful for the help

Reported as a duplicate member of AnT c ++ Feb 13 '18 at 4:56 pm

This question has been marked as a duplicate of an existing one.

  • I would place a one-dimensional array and define the index operators (an additional class will be needed .. to simulate two-dimensionality) so only one placement operation per constructor call and the array will be monolithic - Swift
  • Duplicate . - VTT

1 answer 1

Here you allocate memory -

 int **pa = new int*[this->rows]; // две строки 

and everything would be fine, except that when you exit the function, you get a memory leak ... only with this one member

 private: int **pa; 

It turns out to do with it, not getting any value. And then you try to dereference him ...

  • How to solve this problem? - I. Bilous
  • Is it really incomprehensible? First, remove int** with pa in IntArr(int rows, int cols) ... - Harry
  • Thank you very much for the answer - I. Bilous