Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Debug Assertion Failed! ( Matrix Class)

  1. Apr 29, 2012 #1

    D.Z

    User Avatar

    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 (Text):

    //-------------------------------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
     
  2. jcsd
  3. Apr 29, 2012 #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.
     
  4. Apr 29, 2012 #3

    D.Z

    User Avatar

    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.. :) :)

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Debug Assertion Failed! ( Matrix Class)
  1. Printf debugging (Replies: 3)

  2. Annoying debug messages (Replies: 13)

Loading...