Debug Assertion Failed error - c++

I'm trying to overload the + operator for a project that I'm doing but this keeps on happening. I think the reason is that the object I created is getting deleted when the operator is getting invoked. Not sure how to fix it though. Here's part of my code:
Matrix Matrix::operator+ (const Matrix& m) const
{
//something wrong here
Matrix sum(rows, cols);
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
sum.element[i][j] = this->element[i][j] + m.element[i][j];
}
}
return sum;
}
Additional Information
Matrix::Matrix(int r, int c)
{
rows = r;
cols = c;
element = new int *[c];
for (int i = 0; i < c; i++)
{
element[i] = new int[r];
}
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
element[i][j] = 0;
}
}
}
Matrix::Matrix(const Matrix& m)
{
this->element = m.element;
}
Matrix::~Matrix()
{
//freeing up the arrays
for (int i = 0; i < cols; i++)
{
delete[] element[i];
element[i] = NULL;
}
delete[] element;
element = NULL;
}
Matrix& Matrix::operator= (const Matrix& m)
{
//problem if rows and cols are longer in the new matrix
//need to create a temp matrix to expand to new one
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
this->element[i][j] = m.element[i][j];
}
}
return *this;
}
int* Matrix::operator[] (int n)
{
return element[n];
}
The specific error I am getting is:
Debug Assertion Failed!
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
In line 52 which I did this:
Matrix total = mat + m;
Where mat and m are both object matrices

I'm guessing the problem is caused by this:
Matrix::Matrix(const Matrix& m)
{
this->element = m.element;
}
That is an invalid copy constructor for two reasons. First, you're not initializing rows or cols. Second, you'll now have two Matrix objects pointing to the same memory - both of which will delete it on destruction:
{
Matrix m1(3, 5);
Matrix m2 = m1;
} // BOOM!
You need to do a deep copy here:
Matrix::Matrix(const Matrix& m)
: rows(m.rows), cols(m.cols)
{
element = new int *[cols];
for (int i = 0; i < cols; ++i) {
element[i] = new int[rows];
for (int j = 0; j < rows; ++j) {
element[i][j] = m.element[i][j];
}
}
}

Related

matrix process in c++

For my midterm assignment I am required to code a matrix with all details (like identify any size matrix, add (etc.) a number to matrix, add (etc.) two matrices together, transpose, determinant, print...).
I made a class called Matrix and wrote bunch of constructors and functions and overloaded operators.
#include <iostream>
#include <string>
#include <ctime>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include <cmath>
using namespace std;
class Matrix {
private:
int row;
int column;
int value;
int** matrix;
public:
Matrix(int ro, int col, int val);
Matrix(int ro, int col, char type);
~Matrix();
void print();
void resize(int ro , int col);
void operator=(const Matrix& other);
Matrix operator+(int num)const;
};
Matrix::Matrix(int ro=10, int col=10, int val=0)
:row(ro), column(col), value(val)
{
if (row <= 0 || column <= 0) {
cout << "invalid row or column value";
}
else{
matrix = new int* [row];
for (int i = 0; i < row; i++) {
matrix[i] = new int[column];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrix[i][j] = value;
}
}
}
}
Matrix::Matrix(int ro, int col, char type)
:row(ro), column(col)
{
if (row <= 0 || column <= 0) {
cout << "invalid row or column value";
}
else {
matrix = new int* [row];
for (int i = 0; i < row; i++) {
matrix[i] = new int[column];
}
if (type == 'e'){
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (i == j) {
value = 1;
matrix[i][j] = value;
}
else {
value = 0;
matrix[i][j] = value;
}
}
}
}
srand(time(NULL));
if (type == 'r') {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
value = rand() % 256;
matrix[i][j] =value;
}
}
}
}
}
void Matrix::operator=(const Matrix& other) {
this->resize(other.row, other.column);
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrix[i][j] = other.matrix[i][j]; // this line throw exception:Exception thrown at 0x00A7BCF2 in matrix.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
}
}
}
Matrix Matrix::operator+(int num) const {
Matrix model(row, column);
for (int i = 0; i < model.row; i++) {
for (int j = 0; j < model.column; j++) {
model.matrix[i][j] = matrix[i][j] + num;
}
}
return model;
}
void Matrix::print() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cout<< setw(6) << matrix[i][j]<< " " ;
}
cout << endl;
}
}
Matrix::~Matrix() {
for (int i = 0; i < row; i++) {
delete[] matrix[i];
}
delete[] matrix;
}
void Matrix::resize(int ro, int col){
int** copymatrix;
copymatrix = matrix;
matrix = new int* [ro];
for (int i = 0; i < ro; i++) {
matrix[i] = new int[col];
}
for (int i = 0; i < ro; i++) {
for (int j = 0; j < col; j++) {
if (i >= row || j >= column) {
matrix[i][j] = 0;
}
else {
matrix[i][j] = copymatrix[i][j];
}
}
}
row = ro;
column = col;
}
int main() {
Matrix mat1(3, 6, 2);
cout << endl;
mat1 = mat1 + 5;
mat1.print();
}
I also tried this but gives me the same error
Matrix Matrix::operator=(const Matrix& other) {
Matrix model(other.row, other.column);
for (int i = 0; i < model.row; i++) {
for (int j = 0; j < model.column; j++) {
model.matrix[i][j] = other.matrix[i][j]; //exception
}
}
return model;
}
I actually have problems with operators overloading. As I write in the main function when I use operator+ overloading it returns a matrix and after that it comes to operator= overloading and there is the problem as it returns the matrix and comes to operator= it gives me the error Exception thrown at 0x00CEBCDB in matrix.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD. I think I know why but I don't know how to solve it.
This code works in CodeBlocks 17.12. It throws the error in Visual Studio 2019 (v142).
Your assignment operator doesn't assign new memory for the matrix:
void Matrix::operator=(const Matrix& other)
{
this->resize(other.row, other.column);
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
{
matrix[i][j] = other.matrix[i][j];
}
}
So, after it, you'll be accessing memory through an uninitialized pointer.
You need assignment operators, constructors that correctly allocate memory for your matrices. You need matching destructors, too. Look up "rule of 3".
Matrix& Matrix::operator=(const Matrix& other) {
if (this == &other) {
return *this;
}
this->resize(other.row, other.column);
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
matrix[i][j] = other.matrix[i][j];
}
}
return *this;
}
Matrix Matrix::operator+(int num) const {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
this->matrix[i][j] += num;
}
}
return *this;
}
When Function return a matrix by finishing the scope Destructor get called and after we call assignment operator overloading on it, it can't reach the member of that matrix because it already get deleted by Destructor so it throws a exception.
I wrote this before but it did not work until i realize that we should put (&) for (=) operator if we want use this operators overloading one after the other. so now it's working well

Matrix multiplication with class operator

i am working on multiplication of matrix
it is something like
m5 = m2 * m3;
cout << "m2 * m3 is : " << endl<< m5 << endl;
and this is my code
const Matrix Matrix::operator*(const Matrix &a)
{
Matrix temp(a.row,a.col);
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
for (int k = 0; k<a.row; k++)
{
temp.data[i][j] = temp.data[i][j] + data[i][k] * a.data[k][j];
}
}
}
return temp;
}
However, there is an error always showing at my printing function
ostream& operator<<(ostream &output, const Matrix &a)
{
for (int i = 0; i < a.row; i++)
{
for (int j = 0; j < a.col; j++)
{
output << a.data[i][j] << "\t";
}
output << "" << endl;
}
return output;
}
seems there has a problem on a.data[i][j] which i dont know what's the problem
it works fine on addition.
it is showing an error {data0x005fba90{0xfeeefeee{???}}
can anybody give any advice or suggestions or help on this situation
this is my copy constructor
Matrix::Matrix(const Matrix&m2)
{
row = m2.row;
col = m2.col;
setUp(row, col);
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
data[i][j] = 0;
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
data[i][j] = m2.data[i][j];
}
}
}
this is the set up and default
Matrix::Matrix()
{
row =0;
col = 0;
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
data[i][j] = 0;
}
}
}
Matrix::Matrix(int a, int b, double d[], int c)
{
row = a;
col = b;
setUp(row, col);
int counter = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
data[i][j] = d[counter];
counter++;
}
}
}
and the set up
void Matrix::setUp(int a, int b)
{
row = a;
col = b;
data = new double*[row];
for (int i = 0; i < row; i++) data[i] = new double[col];
}
Without seeing your Matrix constructor, or the class definition, it's hard to say for certain, but if you look at this expression
temp.data[i][j] + data[i][k] * a.data[k][j]
Depending on what data is, if you don't explicitly initialize the data member in the constructor when you create temp then its contents will not automatically be initialized, and the contents will be indeterminate and using it will lead to undefined behavior.
Another possible source of problems might be the lack of a copy-constructor, or a faulty copy-constructor. This is a problem since you return temp by value which invokes the copy-constructor.

implementing Matrix class addition operator overloading weird output

I'm trying to implement a matrix class and overloading the + and = operators.
The problem is I'm getting weird output when I add two matrices as in this picture.
adding two matrices output console
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
int **p, m, n;
public:
Matrix(int row, int col)
{
m = row;
n = col;
p = new int*[m];
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
Matrix (Matrix & x)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j]=x.p[i][j];
}
}
}
~Matrix()
{
for (int i = 0; i < m; i++)
delete [] p[i];
delete [] p;
}
void accept()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cout << setw(10)<<left <<p[i][j] <<" | ";
}
cout << "\n--------------------------------------"<<endl;
}
}
Matrix& operator +(const Matrix & m2)
{
Matrix r(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
r.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return r;
}
Matrix& operator= (const Matrix & eq)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j]=eq.p[i][j];
}
}
return *this;
}
friend Matrix operator * (Matrix, Matrix);
};
Matrix operator* (Matrix a , Matrix b)
{
Matrix B(1,1);
if(a.n == b.m)
{
Matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
B = T;
}
return B;
}
int main()
{
cout << "Enter Matrix 1 (3x2):"<<endl;
Matrix m1(3,2);
m1.accept();
m1.display();
cout << "Enter Matrix 2 (3x2):"<<endl;
Matrix m2(3,2);
m2.accept();
m2.display();
Matrix m3(3,2);
m3=m1+m2;
cout <<endl<< "matrix1 + matix2 is:\n "<<endl;
m3.display();
}
Any ideas how to fix that? I would be grateful for your help and advices with improving it because probably there will be some mistakes.
I use CodeBlocks IDE.
The problem is that your operator + returns a local variable Matrix as a reference; this is undefined behavior. It should be returned by value, not by reference.
Make sure that your Matrix class has a copy constructor that takes a const input, i.e.
Matrix (const Matrix & x)
and that it initializes the array before writing into it. Move the initialization code (the two loops with allocation) into a separate private function, and call it from the default constructor and from the copy constructor.

0xC0000005: Access violation reading location 0xFDFDFDED

So I wrote this:
Matrix.h:
#pragma once
class Matrix
{
private:
int m, n;
double** M = nullptr;
public:
Matrix();
Matrix(int,int);
Matrix(const Matrix&);
~Matrix();
void set_m(int);
void set_n(int);
int get_m() const;
int get_n() const;
void setM(double**,int,int);
double** getM() const;
void show();
Matrix operator*(Matrix&) const;
};
Matrix.cpp:
#include "Matrix.h"
#include<iostream>
using namespace std;
/*
*/
Matrix::Matrix()
{
set_n(1);
set_m(1);
delete[] M;
M = new double*[get_m()];
for (int i = 0; i < m; i++) {
M[i] = new double[get_n()];
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) M[i][j] = 1.0;
}
Matrix::Matrix(int m_,int n_)
{
set_n(n_);
set_m(m_);
delete[] M;
M = new double *[get_m()];
for (int i = 0; i < m; i++) {
M[i] = new double[get_n()];
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) M[i][j] = 1.0;
}
Matrix::Matrix(const Matrix& M_)
{
set_n(M_.get_n());
set_m(M_.get_m());
delete[] M;
M = new double*[get_m()];
for (int i = 0; i < m; i++) {
M[i] = new double[get_n()];
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) M[i][j] = M_.getM()[i][j];
}
void Matrix::set_m(int m_) {
if (m_ > 0)
m = m_;
else
m = 1;
}
void Matrix::set_n(int n_) {
if (n_ > 0)
n = n_;
else
n = 1;
}
double** Matrix::getM() const {
return M;
}
Matrix Matrix::operator*(Matrix &M_) const
{
if (get_n() != M_.get_m()) {
if (get_m() != M_.get_n()) return Matrix();
else {
Matrix T(*this);
return M_*T;
}
}
else {
//n = m
Matrix R(get_m(), M_.get_n());
for (int i = 0; i < get_m(); i++) {
for (int j = 0; j < M_.get_n(); j++) {
double res = 0;
for (int k = 0; k < get_n(); k++) {
res += getM()[i][k] * M_.getM()[k][j];
}
R.getM()[i][j] = res;
}
}
return R;
}
}
int Matrix::get_m() const {
return m;
}
int Matrix::get_n() const {
return n;
}
void Matrix::setM(double** M_, int m_, int n_) {
set_m(m_);
set_n(n_);
for (int i = 0; i < get_m(); i++) {
delete M[i];
}
delete[] M;
M = new double*[m_];
for (int i = 0; i < m; i++) {
M[i] = new double[n_];
}
for (int i = 0; i < m_; i++)
for (int j = 0; j < n_; j++) M[i][j] = M_[i][j];
}
void Matrix::show() {
for (int i = 0; i < get_m(); i++) {
for (int j = 0; j < get_n(); j++) {
cout << getM()[i][j]<<" ";
}
cout << endl;
}
}
Matrix::~Matrix()
{
for (int i = 0; i < get_m(); i++) {
delete M[i];
}
delete[] M;
}
It crashes at void Matrix::setM(...) when I try to delete M[i] for i = 1; it doe perfectly fine for i=0; but crashes later and I do not have a rational explanation why does it do that. I can see the debugger showing me that the memory cannot be read even before it actually crashing but I do not understand how to fix it. Thanks in advance!
You are setting your m before deleting your array based on the old value of m and end up deleting your old array using the new value of m. You should first delete your array, then set mm, and then create your new array.

Two Dimensional Matrices in C++

I'm trying to do some operations in two dimensional matrices. I overloaded (+ , - and *) to do the calculations. I have a problem regarding (I believe) memory management. Look at the following code:
Mtx M1(rows1,cols1,1.0); //call the constructor
Mtx M2(rows2,cols2,2.0); //call the constructor
Mtx M3(rows3,cols3,0.0); //call the constructor
M3 = M1 + M2;
cout << M3 << endl;
Mtx Mtx::operator+(const Mtx &rhs)
{
double **ETS;
ETS = new double*[nrows];
for (int i = 0; i < rhs.nrows; i++) {
ETS[i] = new double[rhs.ncols];
}
if (ETS == NULL) {
cout << "Error Allocation on the Heap" << endl;
exit(1);
}
for (int i = 0; i < rhs.nrows; i++) {
for (int j = 0; j < rhs.ncols; j++) {
ETS[i][j] = 0.0;
}
}
for (int i = 0; i < rhs.nrows; i++) {
for (int j = 0; j < rhs.ncols; j++) {
ETS[i][j] = ets[i][j];
}
}
for (int i = 0; i < rhs.nrows; i++) {
for (int j = 0; j < rhs.ncols; j++) {
ETS[i][j] = ETS[i][j] + rhs.ets[i][j];
}
}
Mtx S(nrows, ncols, ETS);
delete [] ETS;
return S;
}
I think my problem is here:
Mtx S(nrows, ncols, ETS);
delete [] ETS;
return S;
Is this a proper way to return ETS? Or do you think the problem is with the constructor? I got no output when I did the above return!
This is the constructor for Mtx S(nrows, ncols, ETS);
Mtx::Mtx(int rows, int cols, double **ETS)
{
ets = new double*[nrows];
for (int i = 0; i < nrows; i++) {
ets[i] = new double[ncols];
}
for (int i = 0; i < nrows; i++) {
for (int j = 0; j < ncols; j++) {
ets[i][j] = ETS[i][j];
}
}
}
My copy constructor:
Mtx::Mtx(const Mtx& rhs)
:nrows(rhs.nrows), ncols(rhs.ncols)
{
ets = new double*[nrows];
for (int i = 0; i < nrows; i++) {
ets[i] = new double[ncols];
}
for (int i = 0; i < rhs.nrows; i++) {
for (int j = 0; j < rhs.ncols; j++) {
ets[i][j] = rhs.ets[i][j];
}
}
}
I overloaded << to print M3. It works fine because I tested printing M1 and M2.
I also did the following, and still not working:
Mtx S(nrows, ncols, ETS);
for (int i = 0; i < rhs.nrows; i++) {
delete [] ETS[i];
}
delete [] ETS;
return S;
}
You could instead use
std::vector< std::vector<double> >
instead of
double **
that will be more safe as far as bounds are concerned. And you only have to use functions
std::vector::size()
and
std::vector::clear()
in the destructor. :)
Yes, there is a problem where you pointed out. You have to delete all the memory that is pointed to in the pointers in ETS. So, it should be more like:
for (int i = 0; i < rhs.nrows; i++) {
delete [] ETS[i];
}
delete [] ETS;
I good rule is that for each call to new you have to call delete; You allocate the array with nrows+1 calls to new so you have to delete with the same number. That's not hard and fast but it'll clue you in when something is going wrong.
The common way to implement binary arithmetic operators is to define operator+= as a class member function and operator+ as a free-standing function. Note the + operator is implemented in terms of the += operator and it takes its left operand by copy. As long as your copy constructor and assignment operator are correct this should work.
// member function
Mtx& Mtx::operator+=(const Mtx &rhs)
{
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++i) {
ets[i][j] += rhs.ets[i][j];
}
}
return *this;
}
// non-member function
Mtx operator+(Mtx lhs, const Mtx &rhs)
{
lhs += rhs;
return lhs;
}