После окончания работы программа крашится при вызове деструктора
Matrix.h
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>
#include <cstring>
using namespace std;
class DynamicMatrix
{
public:
DynamicMatrix();
DynamicMatrix(int rows, int cols);
DynamicMatrix(const DynamicMatrix& other);
~DynamicMatrix();
void WriteToBinary(ofstream& file);
void ReadFromBinary(ifstream& file);
void SortArrayX(int k);
void SortArrayY(int k);
int** FindElement(int x);
int ReplaceElement(int x, int y, int Number);
DynamicMatrix Sum(const DynamicMatrix& other);
DynamicMatrix Sub(const DynamicMatrix& other);
void operator () (int x, int y, int Number);
DynamicMatrix& operator = (const DynamicMatrix& other);
int* operator [] (int index);
int GetRows();
int GetCols();
int GetElement(int rows, int cols);
char* ToString();
private:
int** arr;
int rows_;
int cols_;
friend DynamicMatrix operator + (const DynamicMatrix& FirstMatrix, const DynamicMatrix& SecondMatrix);
friend DynamicMatrix operator - (const DynamicMatrix& FirstMatrix, const DynamicMatrix& SecondMatrix);
friend ostream& operator << (ostream& out, DynamicMatrix& matrix);
friend istream& operator >> (istream& in, DynamicMatrix& matrix);
};
Matrix.cpp
#include "Matrix.h"
DynamicMatrix::DynamicMatrix(int rows, int cols) : rows_(rows), cols_(cols) {
arr = new int* [rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = rand() % 10;
}
}
}
DynamicMatrix::DynamicMatrix(const DynamicMatrix& other) : rows_(other.rows_), cols_(other.cols_) {
arr = new int* [rows_];
for (int i = 0; i < rows_; i++) {
arr[i] = new int[cols_];
}
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
arr[i][j] = other.arr[i][j];
}
}
}
DynamicMatrix::DynamicMatrix() : DynamicMatrix(0, 0) {}
DynamicMatrix::~DynamicMatrix() {
for (int i = 0; i < rows_; i++) {
delete[] arr[i];
}
delete[] arr;
}
int DynamicMatrix::GetElement(int Rows, int Rols) {
return arr[Rows][Rols];
}
int DynamicMatrix::GetRows() {
return rows_;
}
int DynamicMatrix::GetCols() {
return cols_;
}
char* DynamicMatrix::ToString() {
char* string = new char[rows_ * cols_ * 4];
int index = 0;
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
int length = snprintf(string + index, (rows_ * cols_ * 4) - index, "%d ", arr[i][j]);
index += length;
}
string[index++] = '\n';
}
string[index] = '\0';
return string;
}
void DynamicMatrix::SortArrayX(int k) {
int temp;
for (int i = 0; i < cols_; ++i)
{
for (int j = 0; j < cols_ - 1; ++j)
{
if (arr[k][j] > arr[k][j + 1])
{
temp = arr[k][j];
arr[k][j] = arr[k][j + 1];
arr[k][j + 1] = temp;
}
}
}
}
void DynamicMatrix::SortArrayY(int k) {
int temp;
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < rows_ - 1; ++j)
{
if (arr[j][k] > arr[j + 1][k])
{
temp = arr[j][k];
arr[j][k] = arr[j + 1][k];
arr[j + 1][k] = temp;
}
}
}
}
int** DynamicMatrix::FindElement(int x) {
int** result = new int* [rows_ * cols_ * 2];
int count = 0;
for (int i = 0; i < (rows_ * cols_ * 2 - 1); i++) {
result[i] = new int[2];
}
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
if (arr[i][j] == x) {
result[count][0] = i;
result[count][1] = j;
count++;
}
}
}
return result;
}
int DynamicMatrix::ReplaceElement(int x, int y, int Number) {
if (x >= 0 && y >= 0) {
return arr[x][y] = Number;
}
else {
return -1;
}
}
DynamicMatrix DynamicMatrix::Sum(const DynamicMatrix& other) {
if (rows_ == other.rows_ && cols_ == other.cols_) {
DynamicMatrix result(rows_, cols_);
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
result[i][j] = arr[i][j] + other.arr[i][j];
}
}
return result;
}
}
DynamicMatrix DynamicMatrix::Sub(const DynamicMatrix& other) {
if (rows_ == other.rows_ && cols_ == other.cols_) {
DynamicMatrix result(rows_, cols_);
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
result[i][j] = arr[i][j] - other.arr[i][j];
}
}
return result;
}
else {
std::stringstream stream;
stream << "Matrix have different size :";
throw std::invalid_argument(stream.str().c_str());
}
}
int* DynamicMatrix::operator [] (int index) {
return arr[index];
}
DynamicMatrix& DynamicMatrix::operator = (const DynamicMatrix& other) {
if (&other != this) {
for (int i = 0; i < rows_; i++) {
delete[] arr[i];
}
delete[] arr;
rows_ = other.rows_;
cols_ = other.cols_;
arr = new int* [rows_];
for (int i = 0; i < rows_; i++) {
arr[i] = new int[cols_];
}
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
arr[i][j] = other.arr[i][j];
}
}
}
return *this;
}
void DynamicMatrix::operator () (int x, int y, int Number) {
ReplaceElement(x, y, Number);
}
DynamicMatrix operator + (const DynamicMatrix& FirstMatrix, const DynamicMatrix& SecondMatrix) {
DynamicMatrix Matrix(FirstMatrix);
return Matrix.Sum(SecondMatrix);
}
DynamicMatrix operator - (const DynamicMatrix& FirstMatrix, const DynamicMatrix& SecondMatrix) {
DynamicMatrix Matrix(FirstMatrix);
return Matrix.Sub(SecondMatrix);
}
ostream& operator << (ostream& out, DynamicMatrix& matrix) {
out << matrix.rows_ << " " << matrix.cols_ << endl;
for (int i = 0; i < matrix.rows_; i++) {
for (int j = 0; j < matrix.cols_; j++) {
out << matrix.arr[i][j] << " ";
}
out << endl;
}
return out;
}
istream& operator >> (istream& in, DynamicMatrix& matrix) {
in >> matrix.rows_ >> matrix.cols_;
matrix.arr = new int* [matrix.rows_];
for (int i = 0; i < matrix.rows_; ++i) {
matrix.arr[i] = new int[matrix.cols_];
}
for (int i = 0; i < matrix.rows_; i++) {
for (int j = 0; j < matrix.cols_; j++) {
in >> matrix.arr[i][j];
}
}
return in;
}
Main.cpp
DynamicMatrix Matrix1(5, 5);
DynamicMatrix Matrix2;
ofstream write;
ifstream read;
write.open("Matrix.dat", ios::out | ios::binary);
if (!write.is_open()) {
cout << "File error!" << endl;
return 1;
}
else {
write.write(reinterpret_cast<char*>(&Matrix1), sizeof(DynamicMatrix));
write.close();
}
read.open("Matrix.dat", ios::in | ios::binary);
if (!read.is_open()) {
cout << "File error!" << endl;
return 1;
}
else {
read.read(reinterpret_cast<char*>(&Matrix2), sizeof(DynamicMatrix));
read.close();
}
cout << Matrix2;
Значит хочу я в файл записать, вроде записывается, но программа крашит после завершения. Я вот дебаггером прошелся, увидел то что у нас деструктор второго объекта вызывается для того же адреса что и при первом вызове деструктора, но не могу понять как исправить, помогите пожалуйста!!!!