Memory leak in Soln8_05.cpp from Ivor Horton's Beginning Visual C++ 2008 book?
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:
Attached is the author's solution to exercises 2 through 5. In Soln8_05.cpp, lines 91-100, you have the following:
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?
s1 = s1;
If not, how should it be modified?
5. (Advanced) Overload the + and += operators of your class for concatenating strings.
// 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);
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.