Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

malloc(): corrupted top size when overloading + operator in c++

I’m trying to write a template class function for doing some basic operations with matrices. I have been struggling (a lot, for almost a week ) with memory issues, since it seems like every time i call my constructor class it corrupts the object. I am also suspecting that something is wrong when I define the overload function for the + operator, the most strange thing is that my code works fine with 2×2 matrices but nothing more than that!

I would appreciate if someone has any idea of which could be the problem!
Thank you.

Here is the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include<iostream>
#include<stdlib.h>
#include <stdexcept>
#include<cstring>

using namespace std;

template<class TypeData>
class Matrix{

    private: 
    int Nrows , Ncols;
    TypeData *Array_Matrix;

    public:
    /*Constructors---------*/
    Matrix(){
        Nrows = 0;
        Ncols = 0;
        Array_Matrix = nullptr;
    }
    /*Constructor with 2 parameters:*/
    Matrix(int _Nrows , int _Ncols){
        this -> Nrows = _Nrows;
        this -> Ncols = _Ncols;
        Array_Matrix = new TypeData [Nrows * Ncols];
        for(int i = 0 ; i<Nrows * Ncols ; i++){Array_Matrix[i] = 0.0;}
    }
    /*Copy constructor:*/
    Matrix ( const Matrix &_M){
        this -> Nrows = _M.Nrows ; 
        this -> Ncols = _M.Ncols;
        Array_Matrix = new TypeData [Nrows * Ncols];
        for(int i = 0 ; i<Nrows * Ncols ; i++){*(Array_Matrix + i) = *(_M.Array_Matrix + i);}
    }

    void Set_Value(int row , int col , TypeData value){
        *(Array_Matrix  + Ncols*row + col) = value;}
    /*---------------------------------------------------------------------------------------------------------------------*/

    TypeData Get_Pos_Value(int row , int col){
        if(row >= Nrows || col >= Ncols){throw std::invalid_argument( "Error!, index out of bounds!" );}
        else{return *(Array_Matrix + Ncols*row + col) ;}}

    TypeData *Get_Array(){ /*Method for returning a pointer to the "matrix"*/
        return Array_Matrix;};

    int Get_Nrows(){
        return Nrows;}

    int Get_Ncols(){
        return Ncols;}

    void Print_Matrix(){
        for (int i = 0 ; i<Nrows ; i++){
            cout<<endl;
            for (int j = 0 ; j<Ncols ; j++){
                cout<<*(Array_Matrix + Ncols*i + j)<<" ";}}cout<<endl;}

    /*Operator overloading --------------------------------------------------------------------------------------------*/
    Matrix operator = (const Matrix &Matrix_object){

        delete [] Array_Matrix;
        this -> Nrows = Matrix_object.Nrows;
        this -> Ncols = Matrix_object.Ncols;
        Array_Matrix = new TypeData [Nrows * Ncols];

        for (int i = 0 ; i<Nrows*Ncols ; i++){Array_Matrix [i] = Matrix_object.Array_Matrix[i];}

        return *this;
    }

    Matrix operator + (const Matrix &Matrix_object)const{

        if(Matrix_object.Nrows != Nrows || Matrix_object.Ncols != Ncols)
        {throw std::invalid_argument("Different siex matrices cant be added!" );}

        
        Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
        for(int i = 0 ; i<Nrows*Ncols; i++){
                //Temp.Array_Matrix[i] = Array_Matrix[i] + Matrix_object.Array_Matrix[i];
                *(Temp.Array_Matrix +i) = *(Array_Matrix +i) + *(Matrix_object.Array_Matrix + i);} 
                return Temp;}

   Matrix operator - (const Matrix &Matrix_object)const{

        if(Matrix_object.Nrows != Nrows || Matrix_object.Ncols != Ncols)
        {throw std::invalid_argument("Different size matrices can be substracted!" );}

        Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
        for(int i = 0 ; i<Nrows*Ncols; i++){
                //Temp.Array_Matrix[i] = Array_Matrix[i] - Matrix_object.Array_Matrix[i];
                *(Temp.Array_Matrix +i) = *(Array_Matrix +i) - *(Matrix_object.Array_Matrix + i);}

        return Temp;}

    Matrix operator += (const TypeData num){

        Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
            for(int i = 0 ; i<Nrows*Ncols; i++){
                    Temp.Array_Matrix[i] =  Array_Matrix[i]  + num ;} return Temp;}

    Matrix operator -= (const TypeData num){

        Matrix <TypeData> Temp(0,0); Temp.Nrows = Nrows ; Temp.Ncols = Ncols;
            for(int i = 0 ; i<Nrows*Ncols; i++){
                    Temp.Array_Matrix[i] =  Array_Matrix[i]- num ;} return Temp;}

   ~Matrix(){ delete[] Array_Matrix;}
   };

Main

int main(){
    int r1 , c1; cout<<"Matrix1: Enter Number of rows and columns  "; cin>>r1>>c1;
    Matrix <float> My_Matrix(r1 , c1);

    for (int i = 0 ; i<My_Matrix.Get_Nrows() ; i++){
            for (int j = 0 ; j<My_Matrix.Get_Ncols(); j++){
                float value; cout<<"Type the value at: ["<<i<<"]["<<j<<"] position  "; cin>>value;
                My_Matrix.Set_Value(i , j , value);}}


    int r2 , c2; cout<<"Matrix2: Enter Number of rows and columns   "<<endl; cin>>r2>>c2;
    Matrix <float> My_Matrix2(r2,c2);

    for (int i = 0 ; i<My_Matrix2.Get_Nrows() ; i++){
            for (int j = 0 ; j<My_Matrix2.Get_Ncols(); j++){
                float value; cout<<"Type the value at: ["<<i<<"]["<<j<<"] position  "; cin>>value;
                My_Matrix2.Set_Value(i , j , value);}}


  
    auto My_Matrix3 = My_Matrix + My_Matrix2;
    cout<<"Add:    "<<endl;
    My_Matrix3.Print_Matrix();
    cout<<endl;
    
    cout<<"Testing substraction:    "<<endl;
    auto My_Matrix4 = My_Matrix - My_Matrix2;
    cout<<"Substraction is:  "<<endl;
    My_Matrix4.Print_Matrix();
    cout<<endl; 

    cout<<"Testing = operator:  "<<endl;
    My_Matrix4 = My_Matrix3;
    cout<<"Substraction matriz is now:  "<<endl;
    My_Matrix4.Print_Matrix();
    cout<<endl<<"as the Add matrix! "<<endl;

    cout<<"Testing +=:"<<endl;
    int num ; cout<<"Please enter an integer number:  ";cin>>num;
    (My_Matrix4 += num).Print_Matrix();
    return 0;}

>Solution :

Matrix <TypeData> Temp(0,0); 

Good news: you created a Matrix with 0 rows, 0 columns, and 0 values total.

                              Temp.Nrows = Nrows ; Temp.Ncols = Ncols;

Bad news: the number of rows and columns gets clobbered to something else, then:

        for(int i = 0 ; i<Nrows*Ncols; i++){
                //Temp.Array_Matrix[i] = Array_Matrix[i] + Matrix_object.Array_Matrix[i];
                *(Temp.Array_Matrix +i) = *(Array_Matrix +i) + *(Matrix_object.Array_Matrix + i);} 
                return Temp;}

… the code procedes on the assumption that the Temp.Array_Matrix will have Nrows*Ncols values. Of course, it doesn’t. Hence the memory corruption.

The same bug occurs multiple times in the shown code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading