944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12939
  • C++ RSS
Nov 15th, 2006
0

How to deallocate memory

Expand Post »
Hi Frenz!

Please go through the following program made for matrices:-

cpp Syntax (Toggle Plain Text)
  1. template <class T>
  2. class Matrix
  3. {
  4. private:
  5. int dim1;int dim2;
  6. T **mtx;
  7. public:
  8. Matrix(int a)
  9. {
  10. dim1=dim2=a;
  11. mtx=new T*[dim1];
  12. for(int i=0;i<dim1;i++)
  13. mtx[i]=new T[dim2];
  14. }
  15. ~Matrix()
  16. {
  17. delete []mtx;
  18. }
  19. Matrix<T> operator +(Matrix<T> M)
  20. {
  21. Matrix C(dim1,dim2);
  22. for(int i=0;i<dim1;i++)
  23. for(int j=0;j<dim2;j++)
  24. C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
  25. return C;
  26. }
  27. };

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?
Last edited by ~s.o.s~; Nov 15th, 2006 at 12:02 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harshita_garg is offline Offline
10 posts
since Nov 2006
Nov 15th, 2006
0

Re: How to deallocate memory

Hi, first you have a problem in the operator '+',
C++ Syntax (Toggle Plain Text)
  1. 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.
Last edited by dev.cplusplus; Nov 15th, 2006 at 8:26 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dev.cplusplus is offline Offline
61 posts
since Jun 2006
Nov 15th, 2006
0

Re: How to deallocate memory

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 15th, 2006
0

Re: How to deallocate memory

Also, please use [code] tags.
Reputation Points: 12
Solved Threads: 1
Junior Poster
manutd is offline Offline
100 posts
since Nov 2006
Nov 15th, 2006
0

Re: How to deallocate memory

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harshita_garg is offline Offline
10 posts
since Nov 2006
Nov 15th, 2006
0

Re: How to deallocate memory

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?
Post your entire code along with main( ). Also your code doesnt compile properly. First make your code compilable and then post.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 16th, 2006
0

Re: How to deallocate memory

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 +.
cpp Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. template <class T>
  4. class Matrix
  5. {
  6. private:
  7. int dim1;int dim2;
  8. T **mtx;
  9. public:
  10. Matrix(int a,int b)
  11. {
  12. dim1=a;dim2=b;
  13. mtx=new T*[dim1];
  14. if(mtx==NULL)
  15. cout<<"Memory Exhausted";
  16. for(int i=0;i<dim1;i++)
  17. mtx[i]=new T[dim2];
  18. }
  19. ~Matrix()
  20. {
  21. delete mtx[][];
  22. }
  23. void entervalues()
  24. {
  25. for(int i=0;i<dim1;i++)
  26. for(int j=0;j<dim2;j++)
  27. cin>>mtx[i][j];
  28. }
  29. Matrix<T> operator +(Matrix M)
  30. {
  31. Matrix<T> C(dim1,dim2);
  32. for(int i=0;i<dim1;i++)
  33. for(int j=0;j<dim2;j++)
  34. {
  35. C.mtx[i][j]=mtx[i][j]+M.mtx[i][j];
  36. }
  37. return C;
  38. }
  39. };
  40. void main()
  41. {
  42. Matrix<int> A(2,2),B(2,2),C(2,2);
  43. A.entervalues();
  44. B.entervalues();
  45. C=A+B;
  46. }
Last edited by ~s.o.s~; Nov 16th, 2006 at 11:42 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harshita_garg is offline Offline
10 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help using function to read a file
Next Thread in C++ Forum Timeline: Need some help with Student Grade program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC