Debug Assertion Failed ( Matrix Class)

  • Thread starter Thread starter D.Z
  • Start date Start date
  • Tags Tags
    Class Matrix
AI Thread Summary
The discussion revolves around debugging a C++ Matrix class implementation that crashes at the destructor. The user suspects an issue in the destructor due to improper memory management. Key points include the observation that the code attempts to access unallocated memory when resizing the matrix, particularly in the `setRowNum` method. A recommendation is made to use `std::vector` instead of raw arrays for better memory management and safety. The user later resolves the crashing issue by ensuring sufficient memory allocation for the `arr` pointer. They express a willingness to learn and improve their code by eventually transitioning to using vectors for easier management in future implementations.
D.Z
Messages
2
Reaction score
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
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.
 
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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top