adding two matrix

This is a discussion on adding two matrix within the C++ Programming forums, part of the General Programming Boards category; Following is code for adding two matrix.For a 2 by 2 matrix it is giving first two value as garbage ...

adding two matrix

Following is code for adding two matrix.For a 2 by 2 matrix it is giving first two value as garbage value and next two as same.
The problem is with overloaded+operator function(shaded ).Before returning if I cout the added matrix it prints fine but after returning it is not printing correctly.Even it is not returning *this correctly.

You should also note, however, that the compiler has defined its own assignment operator for your code, and that (AFAIK) it will be a shallow copy. In this case, you would have a memory leak because you allocate memory for m3, then on this line:

Code:

m3=m1+m2;

The pointer is reassigned and you lose the address to the memory you allocated. You should probably define a default constructor that does not allocate any memory, as well as your own assignment operator, which, I suppose, will have to perform a shallow copy. You can then check to see if ptr is valid in the assignment operator, and if it is delete it.

i have not allocated temp (inside greened code) dynamically. I don't this there is any problem in returning temp.Even return(*this) is giving returning this correctly.ie.for a 2*2 matirix first row's value's are garbage.I think return is not working correctly or there is some problem with compiler's overloaded = operator( as i have not supplied my own).I haven't studied vectors yet. So I have to make this through arrays only.

...but in the green, you've dynamically allocated a member of temp when temp calls the constructor.

You pass arguments to functions "by value" or "by reference", but you also return values from functions "by value" or "by reference". You are returning temp "by value" which means temp is copied and the copy of temp is returned. The copy of temp is made with the default copy constructor, which is automatically supplied by the compiler. The default copy constructor makes a "shallow" copy: it just blindly copies member for member. So, the copy of temp has a member that is assigned the address stored in temp's member, i.e. they both point to the same memory.

Now, what happens to the local variables in a function when the function ends? They get destroyed. temp is a local variable in your function; and when the function ends, your temp object calls its destructor which releases its member's dynamically allocated memory. The problem is the copy of temp that is returned from the function has a member that points to the same memory that temp's destructor released. Therefore, the pointer is invalid.

I have understood myself that we passed m1 by value and new variables were created which were destroyed by destructor after returning.Thus *this was also not able to send correct output.
Above program works fine if I remove destructor.

Code:

What neccessary changes should I make
so that above program runs correctly retaining destructor.

The problem is that you have not provided a copy constructor that works correctly. The effect of that is, when you create a copy of a matrix, that you have two matrices referrring to the same dynamically allocated data. Your problem comes in in your code (eg return *this; within your operator+()) because that statement invokes the copy constructor implicitly, and either the original or the copy is destroyed as the function returns. Any attempt to access the data in the surviving object yields undefined behaviour.