When I delete the dynamic arr array, I get a message stating that HEAP CORRUPTION DETECTED, I have been with this problem for several days and will not get to see what's wrong here:

class Matrix { public: int row; int str; vector<TYPE*> matrix; TYPE *arr; } 

Problem function

 void Fill(string fName) { arr = new TYPE[str]; ifstream f(fName.c_str()); if (!f.is_open()) { cout << "Enter the elements:" << endl; for (int i = 0; i < str; i++) { for (int j = 0; j < row; j++) { cin >> arr[j]; } matrix.push_back(arr); } } else { for (int i = 0; i < str; i++) { for (int j = 0; j < row; j++) { f >> arr[j]; } matrix.push_back(arr); } } f.close(); delete[] arr; } 
  • What is this type? - pavel
  • @pavel is a template - WholeHog
  • and falls on all types or only on some? - pavel
  • What is row equal to? Is it guaranteed to not exceed str? - Anton Shchyrov
  • @AntonShchyrov row = 3 and str = 2, and why row should not exceed str? - WholeHog

1 answer 1

The creation of arr is performed once, then it is filled once and saved in martix . In addition to the wrong size for the allocated memory (which was written in the comments), there is also a banal error with overwriting the data. Creating arr needs to be moved inside the for (int i = ...) loop for (int i = ...) , and delete[] removed in principle and called only in the destructor for each matrix element.