How to deallocate memory

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 10
Reputation: Harshita_garg is an unknown quantity at this point 
Solved Threads: 0
Harshita_garg Harshita_garg is offline Offline
Newbie Poster

How to deallocate memory

 
0
  #1
Nov 15th, 2006
Hi Frenz!

Please go through the following program made for matrices:-

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: dev.cplusplus is an unknown quantity at this point 
Solved Threads: 0
dev.cplusplus's Avatar
dev.cplusplus dev.cplusplus is offline Offline
Junior Poster in Training

Re: How to deallocate memory

 
0
  #2
Nov 15th, 2006
Hi, first you have a problem in the operator '+',
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to deallocate memory

 
0
  #3
Nov 15th, 2006
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 100
Reputation: manutd is an unknown quantity at this point 
Solved Threads: 1
manutd's Avatar
manutd manutd is offline Offline
Junior Poster

Re: How to deallocate memory

 
0
  #4
Nov 15th, 2006
Also, please use [code] tags.
Silence is better than unmeaning words.
- Pythagoras
My blog
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 10
Reputation: Harshita_garg is an unknown quantity at this point 
Solved Threads: 0
Harshita_garg Harshita_garg is offline Offline
Newbie Poster

Re: How to deallocate memory

 
0
  #5
Nov 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to deallocate memory

 
0
  #6
Nov 15th, 2006
Originally Posted by Harshita_garg View Post
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 10
Reputation: Harshita_garg is an unknown quantity at this point 
Solved Threads: 0
Harshita_garg Harshita_garg is offline Offline
Newbie Poster

Re: How to deallocate memory

 
0
  #7
Nov 16th, 2006
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 +.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC