Debug Assertion Failed ( Matrix Class)

In summary, the conversation was about debugging a code in C++ and potential issues with accessing arrays. The expert advises using vectors instead of arrays for a simpler and more efficient solution. They also suggest using a proper existing matrix class instead of writing one from scratch.
  • #1
D.Z
2
0
i tried to debug the following code and it crashes in the end at the curly bracket }
i think i have a problem in the destructor .. Can anybody tell me why this happens ??
knowing that the Matrix extends and shrinks as i wish and all the functions work perfectly..
Code:
//-------------------------------Matrix.h-----------------------------------//
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
#ifndef MATRIX_H
#define MATRIX_H

class Matrix {
	friend ostream& operator <<( ostream&, const Matrix& ) ; //insertion operator
	friend istream& operator >>( istream&, Matrix& ) ; //extraction operator
public:
	Matrix(int r=2, int c=3); //Default constructor 
	Matrix( const Matrix& ); //copy constructor 
	~Matrix( ); //Destructor 
	//int getRowNum( ) const; //gets how many rows are there in the matrix
	//int getColNum( ) const; //gets how many colunms are there is the matrix
	//int * getRow( int ) const; //gets a whole row
	//int * getCol( int ) const; //gets a whole column
	void setRowNum( int ); //changes the number of rows in the matrix and deletes the excess rows
	void setColNum( int ); //changes the number of columns in the matrix and deletes the excess columns
	void setRow( int*, int, int ); //sets a whole row to values
	void setCol( int*, int, int ); //sets a whole column to values
	
private:
	int row; 
	int col;
	int ** arr; 
}; 

#endif 
//--------------------------------Matrix.cpp---------------------------//
#include "Matrix1.h"

ostream& operator <<( ostream& out, const Matrix & m )  //insertion operator
{
	for (int i=0; i<m.row; i++)
	{
		for ( int j=0; j<m.col; j++)
			out<<m.arr[i][j]<<" ";
		out << endl;
	}
    out << endl;
	return out; 
}
istream& operator >>( istream& in, Matrix & m )  //extraction operator
{
	for (int i=0; i<m.row; i++)
	{
		for ( int j=0; j<m.col; j++)
			in>>m.arr[i][j]; 
	}
	return in;
}
Matrix::Matrix(int r, int c) //Default constructor
{
	r>0 ? (row=r): exit(1); 
	c>0 ? (col=c): exit(1); 
	arr=new int* [row]; 
	for ( int i=0; i<row; i++)
		arr[i]=new int [col] ; 
	for ( int i=0; i<row; i++)
	{
		for (int j=0; j<col; j++)
			arr[i][j]=0;
	}
}
Matrix::Matrix( const Matrix& m ) //copy constructor
{
	row=m.row; 
	col=m.col; 
	arr=new int* [row]; 
	for ( int i=0; i<row; i++)
		arr[i]=new int [col] ; 
	for ( int i=0; i<row; i++)
	{
		for (int j=0; j<col; j++)
			arr[i][j]=m.arr[i][j];
	}
}
Matrix::~Matrix( ) //Destructor 
{
	for ( int i=0; i<row; i++)
		delete[] arr[i];  
	delete arr;
}
void Matrix::setRowNum( int r ) //changes the number of rows in the matrix and deletes the excess rows
{
	if ( r>row)
	{
		for ( int i=row; i<r; i++)
		{
			arr[i]=new int [col]; 
			for ( int j=0; j<col; j++)
				arr[i][j]=0; 
		}
	}else if ( r<row)
	{
		for ( int i=r; i<row; i++)
			delete [] arr[i]; 
	}
	else 
		cout<<"Invalid!\n";
	row=r; 
}
void Matrix::setColNum( int c ) //changes the number of columns in the matrix and deletes the excess columns
{
	if ( c>col)
	{
		int * temp=new int [c];
		for (int i=0; i<row; i++)
		{
			for ( int j=0; j<col; j++)
				temp[j]=arr[i][j];
			for ( int j=col; j<c; j++)
				temp[j]=0;
			delete [] arr[i];
			arr[i]=new int [c];
			for ( int j=0; j<c; j++)
				arr[i][j]=temp[j];
		}
		delete [] temp; 
	}else if ( c<col )
	{
		int * temp=new int [c];
		for (int i=0; i<row; i++)
		{
			for ( int j=0; j<c; j++)
				temp[j]=arr[i][j];
			delete [] arr[i];
			arr[i]=new int [c]; 
			for ( int j=0; j<c; j++)
				arr[i][j]=temp[j];
		}
		delete [] temp; 
	}
	col=c;
}
void Matrix::setRow( int* newRaw, int size , int r ) //sets a whole row to values
{
	if ( size== col && r<row)
	{
		for ( int i=0; i<row; i++)
		{
			if ( i==r)
			{
				for ( int j=0; j<col; j++)
					arr[i][j]=newRaw[j]; 
			}
		}
	}else 
	{
		cout<<"The new row size must match the columns number\n";
		exit(0);
	}
}
void Matrix::setCol( int* newCol , int size, int c ) //sets a whole column to values
{

	if ( size == row && c<col )
	{
		for ( int i=0; i<row; i++)
		{
			arr[i][c]=newCol[i]; 
		}
	}else 
	{
		cout<<"The new row size must match the columns number\n";
		exit(0);
	}
}
//--------------------------Driver.cpp-----------------------
#include "Matrix1.h"
void main()
{
	Matrix X; 
	cout<<"X:"<< endl<<X; 
	cout<<"Setting X to 5 by 3 matrix \n"<<"X:"<<endl;
	X.setRowNum(5);
	cout<<X;
	cout<<"Setting X to 2 by 3 matrix \n"<<"X:"<<endl;
	X.setRowNum(2);
	cout<<X; 
	int X_row[]={1,2,3};
	X.setRow( X_row, 3, 0); 
	cout<<"Setting the first row in X to values:\n"<<"X:"<<endl; 
	cout<<X;
	cout<<"Setting X with only one column\nX:\n";
	X.setColNum(1);
	cout<<X;
	cout<<"Adding three more columns to X\nX:\n";
	X.setColNum(4);
	cout<<X; 
}
thanks in Advance
 
Technology news on Phys.org
  • #2
It is a very dangerous and usually incorrect assumption that the parts of the code that do not generate the error you just cannot seem to find are bug-free. For example, in Matrix::setRowNum and the case r>row you seem to access arr[row], arr[row+1], ... without having reserved memory for these fields previously. That's merely the first dubious part of your code that I found. I did not go through all of your code.
Since you already use c++ features, I strongly recommend using vectors (std::vector) over arrays, anyways. Usually, I would also recommend using a proper existing matrix class, but in this case I guess the intent is the actual process of writing the class.
 
  • #3
Thank you .. i let arr to arr[20] so that it contains more than enough pointers in case the user wanted to extend the Matrix.. now it is working without errors.. :) :)

Timo said:
Since you already use c++ features, I strongly recommend using vectors (std::vector) over arrays, anyways. Usually, I would also recommend using a proper existing matrix class, but in this case I guess the intent is the actual process of writing the class.

for now this is the only way i know for this class.. in the future when i learn vectors , i think i will discover simpler ways to do this class .. thanks for the advise ..
By the way, this is only part of the code ... i didn't attach the whole thing because it is lengthy..
Thank you
 

1. What does "Debug Assertion Failed (Matrix Class)" mean?

"Debug Assertion Failed (Matrix Class)" is an error message that appears when there is a problem with the code related to a matrix class. It usually means that there is an error in the code that is causing the program to crash or behave unexpectedly.

2. How do I fix a "Debug Assertion Failed (Matrix Class)" error?

Fixing a "Debug Assertion Failed (Matrix Class)" error requires identifying and correcting the code that is causing the error. This could involve checking for typos, making sure the correct data types are being used, or debugging the code to find the source of the error.

3. What can cause a "Debug Assertion Failed (Matrix Class)" error?

There are several potential causes for a "Debug Assertion Failed (Matrix Class)" error, including incorrect syntax, incorrect data types, and memory allocation issues. It could also be caused by a mistake in the logic of the code or a problem with the matrix class itself.

4. Can a "Debug Assertion Failed (Matrix Class)" error be prevented?

While it is not always possible to prevent errors from occurring, there are steps that can be taken to minimize the likelihood of a "Debug Assertion Failed (Matrix Class)" error. This includes writing clean and efficient code, testing the code thoroughly, and using proper debugging techniques.

5. How can I learn more about fixing "Debug Assertion Failed (Matrix Class)" errors?

There are many resources available for learning how to fix "Debug Assertion Failed (Matrix Class)" errors, including online tutorials, forums, and documentation for specific programming languages. It is also helpful to practice writing and debugging code frequently to improve problem-solving skills.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top