### Matrix mult help (pointers)

Hi guys,

Having an issue with matrix multiplication. For the final matrix, I get the proper size for example A[d][e] * B[x][y]= C[d][y].

I also know the algorithm is correct (I found similar ones online) but the damn program keeps crashing, and I have been at it for some time.

Everything else in the program works fine, for example addition, subtraction, copy constructor etc.

Thanks,

Mike

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154`` `````` class Matrix{ public: Matrix(){} Matrix(int a, int b):row(a),col(b){ int i; pointer=new int*[row]; for(i=0;i>(istream& in, Matrix& m); friend ostream& operator<<(ostream& out, Matrix& m); private: int **pointer; int row, col; }; int main(){ Matrix m1(4,2), m2(2,4); cin>>m1; cout<>m2; cout<>(istream& in, Matrix& m){ for(int i=0;i>m.pointer[i][j]; } } return in; }``````
Crashing when working with arrays is mostly due to two reasons: accessing restricted memory cells and memory leakage (I'm not 100% if the terms are technically correct).

You only used `delete `in the destructor, what about the functions that return a matrix? Don't you want to delete your temporary matrix?

Addition and subtractions work without the delete, I would imagine that multiplication should work the same way. I also don't recall reading in the book I am going through right now anything about having to delete anything from each overloaded function.

You're right I wanted to ask whether addition and subtraction crashed your application. Anyway the algorithm seems right, and I even tried rewriting the multiplication manually inside main and without using classes, there was no problem.

Also, I don't get how you're assuming values are initialized to 0. And, how didn't this give you compile errors?
`Matrix operator=(Matrix& m)`

It should be like this

`Matrix & operator=(const Matrix& m)`
Last edited on
Does multiplication work for you only inside main? How about the function?

Mike
Okay, I've been testing for the last hour now :D

I fixed something:

 ``123456`` ``````for(i=0;i

And I make sure the multiplication operator returns correctly. For some reason, there seems to be a whooole lot of integers in the io buffer that come up all at once when using any cout... Couldn't figure out why, and I probably won't because I never use the `new[]` operator...

If your only interested in the algorithm itself rather than practicing C++ dynamic arrays, go ahead and use vector in STL.
Last edited on
Thanks a lot for the help. I was looking however to returning the object, but what you suggest should be fine for now. I think with a little more experience, I will come back to this and try to figure it out again.

Mike
Topic archived. No new replies allowed.