Hi Frenz!

Please go through the following program made for matrices:-

template <class T>
class Matrix
{
 private:
      int dim1;int dim2;
      T **mtx;
public:
 Matrix(int a)
 {
      dim1=dim2=a;
      mtx=new T*[dim1];
      for(int i=0;i<dim1;i++)
      mtx[i]=new T[dim2];
 }
 ~Matrix()
 {
  delete []mtx;
 }
 Matrix<T> operator +(Matrix<T> M)
 {
      Matrix C(dim1,dim2);
      for(int i=0;i<dim1;i++)
      for(int j=0;j<dim2;j++)
      C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
      return C;
  }
};

Problem with this program is that it creates a temporary matrix C in the function operator+ . This Matrix C calls the destructor before its value can be returned to the matrix, i want to store the result into. Because of this, the program stores random values when matrices are added. I can not remove the destructor because I must release the memory alloted to the temporary matrix C else the total memory is exhausted. I also tried to release the memory in another function but I dont know how & where to call this function. Can somebody please suggest some solution to my problem? :sad:

Hi, first you have a problem in the operator '+',

Matrix C(dim1,dim2);

You try to use a constructor with two paramenters and you have a constructor only with one parameter.
The reason why the constructor is called before you can return it, is because you are in the end of the method '+', so the life of the temporary object in the '+' ends, when the return is called, the destructor was already called.

The destructor is not deallocating enough memory -- it heas a memory leak. For every new[] there must be a corresponding delete[]. You need to add a loop similar to the one that is in the constructor.

As for your problem -- hummm I'll have to think about it awhile. No time right now. Maybe someone else can answer sooner.

Also, please use [code] tags.

The program actually has 3 constructors : 1 with 2 arguments, 1 with 1 argument & 1 with no arguments. I have actually attached a very small part of my code here thats why i forgot to include the constructor required here. So the problem is not wht is pointed out by dev.cplusplus.

Problem with this program is that it creates a temporary matrix C in the function operator+ . This Matrix C calls the destructor before its value can be returned to the matrix, i want to store the result into. Because of this, the program stores random values when matrices are added. I can not remove the destructor because I must release the memory alloted to the temporary matrix C else the total memory is exhausted. I also tried to release the memory in another function but I dont know how & where to call this function. Can somebody please suggest some solution to my problem? :sad:

Post your entire code along with main( ). Also your code doesnt compile properly. First make your code compilable and then post.

Here is the complete compilable code u asked for.But the problem remains the same. How to deallocate the memory of a temporary variable like C in teh function operator +.

#include<iostream.h>
#include<conio.h>
template <class T>
class Matrix
{
   private:
      int dim1;int dim2;
      T **mtx;
   public:
      Matrix(int a,int b)
      {
  dim1=a;dim2=b;
  mtx=new T*[dim1];
 if(mtx==NULL)
    cout<<"Memory Exhausted";
 for(int i=0;i<dim1;i++)
    mtx[i]=new T[dim2];
      }
~Matrix()
{
   delete mtx[][];
}
      void entervalues()
      {
  for(int i=0;i<dim1;i++)
     for(int j=0;j<dim2;j++)
        cin>>mtx[i][j];
      }
      Matrix<T> operator +(Matrix M)
      {
  Matrix<T> C(dim1,dim2);
  for(int i=0;i<dim1;i++)
     for(int j=0;j<dim2;j++)
     {
        C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
     }
  return C;
   }
};
void main()
{
   Matrix<int> A(2,2),B(2,2),C(2,2);
   A.entervalues();
   B.entervalues();
   C=A+B;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.