448,672 Members | 1,628 Online Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

 P: n/a The code below I suspect creates memory leaks: In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. After "c += 'new_object'" we have an unreferenced object which created from "a + b" and not destroyed. am I right? What I can do to avoid this? ------------------------------ class Vector3D { protected: float _x, _y, _z; public: Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {} const Vector3D &operator+=(const Vector3D &a); friend const Vector3D operator+(const Vector3D &a, const Vector3D &b); }; const Vector3D &Vector3D::operator+=(const Vector3D &a) { _x += a._x; _y += a._y; _z += a._z; return *this; } const Vector3D operator+(const Vector3D &a, const Vector3D &b) { return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z); } void main() { Vector3D a = Vector3D(1, 2, 3); Vector3D b = Vector3D(4, 5, 6); Vector3D c = Vector3D(0, 0, 0); c += a + b; } Jul 19 '05 #1
9 Replies

 P: n/a Not OperatION, operatOR. Sorry! The code below I suspect creates memory leaks: In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. After "c += 'new_object'" we have an unreferenced object which created from "a + b" and not destroyed. am I right? What I can do to avoid this? ------------------------------ class Vector3D { protected: float _x, _y, _z; public: Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {} const Vector3D &operator+=(const Vector3D &a); friend const Vector3D operator+(const Vector3D &a, const Vector3D &b); }; const Vector3D &Vector3D::operator+=(const Vector3D &a) { _x += a._x; _y += a._y; _z += a._z; return *this; } const Vector3D operator+(const Vector3D &a, const Vector3D &b) { return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z); } void main() { Vector3D a = Vector3D(1, 2, 3); Vector3D b = Vector3D(4, 5, 6); Vector3D c = Vector3D(0, 0, 0); c += a + b; } Jul 19 '05 #2

 P: n/a "<- Chameleon ->" wrote in message news:bk**********@nic.grnet.gr... The code below I suspect creates memory leaks: In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. After "c += 'new_object'" we have an unreferenced object which created from "a + b" and not destroyed. am I right? No. You don't have to worry about a memory leak; temporary objects will be automatically destroyed after the expression which caused them to be created has been fully evaluated. -- Peter van Merkerk peter.van.merkerk(at)dse.nl Jul 19 '05 #3

 P: n/a On Sat, 20 Sep 2003 21:06:41 +0300, "<- Chameleon ->" wrote: The code below I suspect creates memory leaks: Nope. But it's prone to have other bugs since you don't use indentation. In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. The new object (if any, this depends on what optimizations the compiler throws in; read about RVO) is a temporary one, on the stack, and it's discarded after its value is assigned to 'c'. After "c += 'new_object'"we have an unreferenced object which created from "a + b" and not destroyed.am I right? No. What I can do to avoid this? Write correct code. ------------------------------class Vector3D {protected:float _x, _y, _z; Don't use leading underscores. In C++ names beginning with leading underscore followed by uppercase letter are reserved for the implementation. In C there are even more restrictions. If you don't have a lot of vectors at the same time, consider using 'double' instead of 'float'. 'double' has better precision and on modern machines is as fast as, or even faster than, 'float'. public:Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {} Do use indentation, and also some blank lines. const Vector3D &operator+=(const Vector3D &a); friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);};const Vector3D &Vector3D::operator+=(const Vector3D &a) {_x += a._x; _y += a._y; _z += a._z; return *this;} OK except for formatting and names. const Vector3D operator+(const Vector3D &a, const Vector3D &b) {return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z);} OK except for formatting and names. void main() { Non-standard extension. Use a standard 'main'. Vector3D a = Vector3D(1, 2, 3);Vector3D b = Vector3D(4, 5, 6);Vector3D c = Vector3D(0, 0, 0);c += a + b;} To be honest, Vector3D looks like someone else's code reformatted. Jul 19 '05 #4

 P: n/a > >The code below I suspect creates memory leaks: Nope. But it's prone to have other bugs since you don't use indentation. Thanks for your response. What is indentation? I don't understand the word ;-( What I can do to avoid this? Write correct code. lol ;-) Do use indentation, and also some blank lines. OK except for formatting and names. I "pack" the code to avoid a big message. Vector3D a = Vector3D(1, 2, 3);Vector3D b = Vector3D(4, 5, 6);Vector3D c = Vector3D(0, 0, 0);c += a + b;} To be honest, Vector3D looks like someone else's code reformatted. grrrrrr! of course its mine! I am trying to test operator overloading because I read "Thinking in C++" Because I use OpenGL, I am trying to write (what else) a Vector3D class Jul 19 '05 #5

 P: n/a To be honest, Vector3D looks like someone else's code reformatted. That's a good way to learn. I recommend it to every student. Reformat someone else's code. <- Chameleon -> What is indentation? I don't understand the word ;-( It's an old C++ joke: "The road to perdition is paved with good indentation". It means that you must set your tab width to zero. #include class Vector3D { public:Vector3D(double a,double b,double c):x(a),y(b),z(c) { cout<<"\nVector3D("<

 P: n/a > It means that you must set your tab width to zero. Zero tab width???!!! You mean this ---- while (1) { doit(); } ---- instead of this? ---- while(1) { doit(); } ---- Never!!! In the code below, why you use inline functions? #include class Vector3D { public:Vector3D(double a,double b,double c):x(a),y(b),z(c) { cout<<"\nVector3D("<

 P: n/a <- Chameleon -> Zero tab width???!!! You mean this ---- while (1) { doit(); } ---- instead of this? ---- while(1) { doit(); } ---- Never!!! Try while(1)doit(); -X Jul 19 '05 #8

 P: n/a Peter van Merkerk wrote: temporary objects will be automatically destroyed after the expression which caused them to be created has been fully evaluated. After the *full expression* is finished. A full expression is an expression which is not part of a larger expression. -Kevin -- My email address is valid, but changes periodically. To contact me please use the address from a recent posting. Jul 19 '05 #9

 P: n/a "<- Chameleon ->" wrote in message news:bk**********@nic.grnet.gr... [...] What is indentation? I don't understand the word ;-( [...] Inserting spaces and tabs into source code so that it's more readable. // code with no indentation for(int k=0;k<3;k++) {std::cout<

### This discussion thread is closed

Replies have been disabled for this discussion. 