Results 1 to 6 of 6
I am trying to make a vector class with some functions and operators, and I was wondering if the following would cause a memory leak:
Code:
class vector
{
private:
...
- 08-07-2007 #1Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
is this a memory leak?
I am trying to make a vector class with some functions and operators, and I was wondering if the following would cause a memory leak:
This statement will run millions of times so I want to make sure that it's not leaking memory all over the place. Any help on this would be much appreciated.Code:class vector { private: int size; double *value; public: vector(vector&); vector(int); vector() { value = NULL; }; ~vector () {delete [] value;}; int get_size() { return size; }; double &operator () (int a) { return (value[a]); }; vector &operator + (vector&); }; vector::vector (int a) { size = a; value = new double[size]; } vector::vector (vector& a) { size = a.get_size(); value = new double[size]; for (unsigned i=0; i<size; ++i) value[i] = a(i); } vector &vector::operator+ (vector& a) { vector temp(size); for (unsigned i=0; i<size; ++i) temp(i) = value[i] + a(i); return (*(new vector(temp))); // does this statement cause a leak??? }
- 08-07-2007 #2Just Joined!
- Join Date
- Aug 2007
- Posts
- 33
#run the program & check the resources being used by the command
lsof -p process-id
- 08-07-2007 #3Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
it does leak
I ran some simple tests and it is causing a memory leak.
But how can I change the + operator function to not leak memory?
I've tried many other varieties but none of them work other than returning the local variable itself (which is not a good way to do it anyways).
- 08-08-2007 #4Just Joined!
- Join Date
- Aug 2007
- Posts
- 7
Memory Leak
you can change the Prottotype of the overloaded operator to:-
vector operator +(vector &v){
vector temp;
return temp;
}
for this you also need to overload = operator.
- 08-08-2007 #5Just Joined!
- Join Date
- Aug 2007
- Posts
- 3
I tried your suggestion, but it won't compile now.
This code:
Gives the error message:Code:#include <iostream> class vector { private: int size; double *value; public: vector(vector&); vector(int); vector() { value = NULL; }; ~vector () {delete [] value;}; int get_size() { return size; }; double &operator () (int a) { return (value[a]); }; vector operator +(vector a){ vector temp(size); for (unsigned i=0; i<size; ++i) temp(i) = value[i] + a(i); return temp; } vector &operator =(vector& a){ for (unsigned i=0; i<size; ++i) value[i] = a(i); return *this; } }; vector::vector (int a) { size = a; value = new double[size]; } vector::vector (vector& a) { size = a.get_size(); value = new double[size]; for (unsigned i=0; i<size; ++i) value[i] = a(i); } int main(void) { vector x(100); vector y(100); for (unsigned i=0; i<100; ++i) { x(i) = 3.0; y(i) = 0.0; } y = x+x; return 0; }
Code:In function 'int main()': error: no match for 'operator=' in 'y = vector::operator+(vector)(vector(((vector&)(& x))))' note: candidates are: vector& vector::operator=(vector&)
- 08-09-2007 #6Just Joined!
- Join Date
- Aug 2007
- Posts
- 7
sorry Its my Bad, Please comment the code :-
/* vector &operator =(vector& a){
for (unsigned i=0; i<size; ++i)
value[i] = a(i);
return *this;
}
*/
there is no need to overload the = Operator.


Reply With Quote