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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63

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

    Hi,

    I've been working through Ivor Horton's Beginning Visual C++ 2008 book and have made good progress so far. However, I've come across what looks to be a memory leak in one of the exercise solutions and I'm sure it's got to be me that's wrong.

    In chapter 8, exercises two through five you're asked:

    2. Implement a simple string class in native C++ that holds a char* and an integer length as private data members. Provide a constructor which takes an argument of type const char*, and implement the copy constructor, assignment operator and destructor functions. Verify that your class works. You will find it easiest to use the string functions from the <cstring> header file.

    3. What other constructors might you want to supply for your string class? Make
    a list, and code them up.

    4. (Advanced) Does your class correctly deal with cases such as this?
    string s1;
    ...
    s1 = s1;
    If not, how should it be modified?

    5. (Advanced) Overload the + and += operators of your class for concatenating strings.
    Attached is the author's solution to exercises 2 through 5. In Soln8_05.cpp, lines 91-100, you have the following:

    Code:
    // Addition operator: add two CSimpleString objects
    CSimpleString CSimpleString::operator+(const CSimpleString& s)
    {
      size_t length = len + s.len + 1;
       char* tmp = new char[length];
       strcpy_s(tmp, length, buff);
       strcat_s(tmp, length, s.buff);
    
       return CSimpleString(tmp);
    }

    Am I right in that the memory for tmp is never deleted?



    I thought that maybe since it was passing out of scope that it would be automatically "freed", but that only applies to automatic variables. Then I thought that maybe because it was a pointer to char* that it had static duration, but no, that applies to string literals.

    Thanks for any/all thoughts on this.
    Attached Files Attached Files
    Last edited by deoren; 01-09-2012 at 06:56 PM. Reason: Added missing attachment
    It is better to fail with honor than win by deceit
    - unknown

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 11-27-2011, 11: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, 07:57 PM
  4. answers for Ivor Horton's Beginning C++?
    By 7stud in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 12:13 AM
  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