0

I created a template class which named with Matrix. I'm trying to figure out how to implement an operator+ overload for a pointer which is pointed an Matrix object. My object is scalar matrix operations. I want to add an integer value to each cells on the matrix. I could not understand what is going on my code. The templates have confused me a lot.

"m1" is my Matrix object. When I use m1 = m1+2, I'm getting segmentation fault error.

I have tried many different combinations, but I get the same error all the time.

  • I tried without a pointer.
  • I tried typing int instead of T.
  • Instead of directly returning new Matrix <int> (10,10,2), I tried:
Matrix <int> * m = new Matrix <int> (10,10,2)
return m;
  • When I delete second m1->print(); statement in main function, the "segmentation fault" error disappears, but every time I try to print I get an error again.

My Matrix class:

template <typename T> 
class Matrix{
    vector< vector<T> > matris;
public: 
    Matrix(); // 10x10 matrix with zeros
    Matrix(int width, int height, int value); 
    void print();

    Matrix* operator+(T value){
        return (new Matrix<int>(10,10,2)); // 10x10 matrix filled with 2
    }
};

My main function:

int main(){
    Matrix <int> *m1 = new Matrix <int>(); // 10x10 matrix with zeros
    m1->print(); // it succesfully shows the elements
    m1 = m1 + 2; // whenever I tried this, I get "Segmentation Fault"
    m1->print();

    return 0;
}

My print() function:

template <typename T> 
void Matrix<T>::print(){
    for(int h=0; h<matris.size(); h++){
        for(int w=0; w<matris[0].size(); w++){
            printf("%4d", matris[h][w]);
        }
        cout << endl;
    }
}

Output:

   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
Segmentation fault

My expectation is that the assignment was successful, but I get the segmentation fault error. What am I doing wrong here?

Onur
  • 3
  • 1
  • 3
    This `m1 = m1 + 2;` just adds `2` to `m1` in arithemtic pointer. It would work if you had array of matrices, but you have only one Matrix instance allocated. You overloaded `operator+` for `Matrix` not `Matrix*`, so it should look like `m1 = *m1 + 2`; – rafix07 Oct 16 '19 at 04:50
  • Be aware that you *cannot* overload arithmetic operators for pointers. – Aconcagua Oct 16 '19 at 05:57

1 Answers1

2

When you use

 m1 = m1 + 2;

the overloaded operator+ is not used. That is just pointer arithmetic. After that, m1 points to memory that you don't own. Accessing members of m1 after that causes undefined behavior.

You can fix that syntactically by using

 m1 = *m1 + 2;

but it has its own problems. When you do that, the original memory m1 was pointing to is lost to your program. You have a memory leak.

Providing the overload as

Matrix* operator+(T value) { ... }

is not idiomatic. Perhaps you want to use:

Matrix operator+(T const& value) const { ... }

A better alternative would be to use couple of non-member function overloads:

Matrix operator+(Matrix const& m, T const& value);
Matrix operator+(T const& value, Matrix const& m);

With that, you can use:

Matrix<int> m1;
Matrix<int> m2 = m1 + 10;
Matrix<int> m3 = 30 + m1;

More on operator overloads can be found at What are the basic rules and idioms for operator overloading?

R Sahu
  • 204,454
  • 14
  • 159
  • 270