| | |
How to deallocate memory
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 10
Reputation:
Solved Threads: 0
Hi Frenz!
Please go through the following program made for matrices:-
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?
Please go through the following program made for matrices:-
cpp Syntax (Toggle Plain Text)
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?
Last edited by ~s.o.s~; Nov 15th, 2006 at 12:02 pm.
Hi, first you have a problem in the operator '+',
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.
C++ Syntax (Toggle Plain Text)
Matrix C(dim1,dim2);
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.
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.
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.
•
•
•
•
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?
I don't accept change; I don't deserve to live.
•
•
Join Date: Nov 2006
Posts: 10
Reputation:
Solved Threads: 0
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)
#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; }
Last edited by ~s.o.s~; Nov 16th, 2006 at 11:42 am.
![]() |
Similar Threads
- free() = delete or delete[]? (C++)
- Winsock Multi-Client Servers (C++)
- necessarry or not (memory managment) (C++)
- Allocating Memory (C++)
- Function with pointer? (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help using function to read a file
- Next Thread: Need some help with Student Grade program
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






