Memory leak in Soln8_05.cpp from Ivor Horton's Beginning Visual C++ 2008 book?

This is a discussion on Memory leak in Soln8_05.cpp from Ivor Horton's Beginning Visual C++ 2008 book? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by deoren That makes sense to me. The rest, not so much. I'll take your word for it ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Quote Originally Posted by deoren View Post
    That makes sense to me. The rest, not so much. I'll take your word for it until I learn more and have a chance to see it in action. This particular chapter is making my head hurt.
    Well, this of it this way...

    If we have an integer, x, and an integer y, we can either say "take x and add y and store the result in z." This is written as

    int z = x + y;

    We can also say, "take integer x, add y and store the result in x." This is written as:

    x += y;
    (x = x + y)

    So += would modify its left-hand variable, x, in this case.
    Operator + does not modify its operands, since it returns a temporary.
    Notice the "the result." This is the temporary part, which is the operation of adding x and y (but it isn't saving that anywhere!).

    ...should I have

    Code:
       return temp_obj;
    instead?

    Since I'm not returning a pointer, but an object by value, I'm thinking the second approach is fine?

    Thanks in advance for any help.
    Yeah, we basically want to make a temporary object with the new string and return that.
    temp_obj is the temporary object we want to return, so no "wrapping" is necessary.
    (If I followed this right.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Quote Originally Posted by Elysia View Post
    Yeah, we basically want to make a temporary object with the new string and return that.
    temp_obj is the temporary object we want to return, so no "wrapping" is necessary.
    (If I followed this right.)
    Awesome. I'm glad, as that made the most sense to me. It looks like the "wrapping" would work, but would be inefficient.



    Quote Originally Posted by Elysia View Post
    Well, this of it this way...

    If we have an integer, x, and an integer y, we can either say "take x and add y and store the result in z." This is written as

    int z = x + y;

    We can also say, "take integer x, add y and store the result in x." This is written as:

    x += y;
    (x = x + y)

    So += would modify its left-hand variable, x, in this case.
    Operator + does not modify its operands, since it returns a temporary.
    Notice the "the result." This is the temporary part, which is the operation of adding x and y (but it isn't saving that anywhere!).
    I'm with you on that, and that makes sense.

    But what I'm having trouble with is understanding laserlight's mention of using operator+= to implement operator+.

    If I didn't know that you could override operator+ to actually do subtraction if you were of a devious sort, I would expect the operator+ function to be used for only implementing a function that:

    • Does not modify its arguments
    • Returns a copy of a new object


    If I use an overloaded += inside of the operator+ function, have the operator+= function setup where it modifies the object, wouldn't something like
    Code:
    str1 = str2 + str3
    result in str2 being modified?

    So, if I start with (Soln8_05.cpp, r1) as the starting point for wrapping my head around the logic and setup the operator+= function to modify the object, how would I be able to use that to implement the operator+ function?

    The only thing I come up with is deciding that operator+= really doesn't modify the object in-place, so I know I have some work to do before I understand it. It's probably something simple, but I'm not seeing it.

    Thanks for the help!
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Well, first, let's remember what x+= y means.
    It means x = x + y.
    Now, as you see, we can split this into two operations.
    One addition and one assignment.
    So you can implement it as that.
    Add x and y together with operator +, then assign it to x.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Quote Originally Posted by Elysia View Post
    Well, first, let's remember what x+= y means.
    It means x = x + y.
    Now, as you see, we can split this into two operations.
    One addition and one assignment.
    So you can implement it as that.
    Add x and y together with operator +, then assign it to x.
    Thanks for the post.

    I still don't get how operator+ can be implemented with operator+=, but will understand it later at some point. I expect I just need to practice some more and then a light bulb should come on.
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    You don't need to do it with classes right away.
    Try it with normal integers first. Once you get the idea, it should be easy to implement in your class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Quote Originally Posted by deoren View Post
    I still don't get how operator+ can be implemented with operator+=
    Operator + internally creates a new string object as a copy from itself, then appends the other string (using operator +=) and then returns that newly created string.

  7. #22
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Quote Originally Posted by Caligulaminus View Post
    Operator + internally creates a new string object as a copy from itself, then appends the other string (using operator +=) and then returns that newly created string.
    Nice, that makes sense to me. Thanks for explaining that. I'm not sure why I couldn't see that.

    Sort of an off-topic question:

    Now that everyone has so graciously answered my questions on this thread (seriously, thanks a lot to everyone!), should I create a new thread if I have further questions about Soln8_05.cpp or continue here?

    My next two questions involve why g++ and MSVS use different approaches for handling this line:

    Code:
    s1 += " world!";
    Thanks.
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    I'd say that's a split opinion, but in my opinion, you should make a new thread since that way it might be easier for people searching to find a similar problem to their own.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    I'll do that, thanks.

    Edit: Started this thread.
    Last edited by deoren; 01-14-2012 at 10:28 AM. Reason: Adding link to spin-off thread
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 11-27-2011, 10:44 AM
  2. Detecting Memory leak in Visual Studio (Debug mode)
    By elizas in forum Windows Programming
    Replies: 3
    Last Post: 05-06-2010, 11:14 AM
  3. how to deteck memory leak in visual c++?
    By franziss in forum Windows Programming
    Replies: 16
    Last Post: 11-11-2009, 06:57 PM
  4. answers for Ivor Horton's Beginning C++?
    By 7stud in forum C++ Programming
    Replies: 1
    Last Post: 03-16-2005, 11:13 PM
  5. "Ivor Horton's Beginning ANSI C++" Wacha think?
    By Zeusbwr in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2004, 07:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21